从Ajax请求Spring MVC。 404错误

时间:2022-10-07 14:32:19

I am new in Spring MVC and I have problem with request. I got 404 error in my browser console. What should I change in my code? I think that xml files are wrong. i would be grateful for your reply

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

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>

1 个解决方案

#1


0  

url in AJAX should be: "loginUser.html" accordingly to <url-pattern>*.html</url-pattern> in web.xml

AJAX中的url应该是:“loginUser.html”相应于web.xml中的 * .html

#1


0  

url in AJAX should be: "loginUser.html" accordingly to <url-pattern>*.html</url-pattern> in web.xml

AJAX中的url应该是:“loginUser.html”相应于web.xml中的 * .html