从Ajax请求Spring MVC。 404错误

时间:2016-04-23 14:31:36

标签: ajax spring spring-mvc

我是Spring MVC的新手,我的请求有问题。 我的浏览器控制台出现404错误。我应该在代码中更改什么?我认为xml文件是错误的。我很感激你的回复

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>

1 个答案:

答案 0 :(得分:0)

AJAX中的

a = comb.flat_map { |s| s.split /\s+=>\s+/ } #=> ["b", "c", "c", "f", "f", "b"] b = a.rotate #=> ["c", "c", "f", "f", "b", "b"] enum = a.each_slice(2) #=> #<Enumerator: ["c", "c", "f", "f", "b", "b"]:each_slice(2)> enum.to_a #=> [["c", "c"], ["f", "f"], ["b", "b"]] enum.all? { |l,f| l==f } #=> true 应该是:url相应于"loginUser.html"中的<url-pattern>*.html</url-pattern>