2016-04-23 98 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> 

+0

它不工作:/ –

+0

你部署你的应用程序到Tomcat或码头的东西?如果是这样,你应该预先安排你的上下文路径到每个网址..也添加更多关于你的项目结构和配置的细节 –

+0

我正在使用Glassfish。 –

回答

0

url在AJAX应该是:"loginUser.html"相应地<url-pattern>*.html</url-pattern>web.xml