2016-04-23 37 views
0

私はSpring MVCで新しく、リクエストに問題があります。 ブラウザのコンソールで404エラーが発生しました。コードで何を変更すればよいですか?私はXMLファイルが間違っていると思います。私はあなたの返事をAjaxからSpring MVCへのリクエスト。 404エラー

function login() { 
var data = {}; 
var url = "/loginUser"; 
data["name"] = $("#loginName").val(); 
data["password"] = $("#loginPassword").val(); 
$.ajax({ 
    type: "GET", 
    url: url, 
    data: 'name=' + data['name'] + '&password=' + data["password"], 
    success: function() { 
     console.log("Success") 
    }, 
    error: function (e) { 
     console.log("ERR") 
     //... 
    } 
}); 
} 

Spring MVCの感謝される:

@Controller 
public class AjaxLoginController { 
@RequestMapping(value = "/loginUser", method = RequestMethod.GET) 
public @ResponseBody String loginUser(@RequestParam(value="name") String name, @RequestParam(value="password") String password) { 
    System.out.println(name); 
    return "" ; 
    } 
} 

ディスパッチャ-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?> 
    <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> 
    <beans 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 

     xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 


<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="login.html">indexController</prop> 
     </props> 
    </property> 
</bean> 

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 

<bean name="indexController" 
     class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
     p:viewName="login" />  

<mvc:annotation-driven /> 
<mvc:resources location="resources/" mapping="/resources/**" /> 
<mvc:resources location="WEB-INF/" mapping="/WEB-INF/**" /> 

web.xmlの

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
     <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
<listener> 
    <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 
AJAXで

+0

<url-pattern>*.html</url-pattern>に応じ"loginUser.html":/ –

+0

あなたは、Tomcatや桟橋か何かにアプリを展開していますか?もしそうなら、あなたはあなたのコンテキストパスをすべてのurlに前置する必要があります。また、あなたのプロジェクトの構造と設定に関する詳細を追加してください。 –

+0

私はGlassfishを使用しています。 –

答えて

0

urlする必要があります:それは動作しませんweb.xml

関連する問題