从Ajax到Spring MVC的请求。404错误

卡罗尔·比利基(Karol Bilicki)

我是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")
        //...
    }
});
}

春季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 "" ;
  }
}

dispatcher-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>

卡罗尔·比利基(Karol Bilicki)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章