如果我将url作为链接提供,那么当我在Ajax中提供相同的url时,它会触及控制器方法,但它不会转到它。它会在控制器中点击上一个方法并重新加载主页。
我的web.xml是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
我的spring-servlet.xml是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan
base-package="in.ac.nitk.tas.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
我按下登录按钮
调用此功能function submitLoginForm(){
var data ={ userName: "me",
password:"pas"
};
$.ajax({
type: 'POST',
url: '/home/login.do',
cache:false,
data:JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
alert("success " );
},
error: function (req, status, error) {
alert("R: " + req + " S: " + status + " E: " + error);
}
});
}
我的控制器是这样的
package in.ac.nitk.tas.controller;
import in.ac.nitk.tas.service.HomeService;
import in.ac.nitk.tas.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/home")
public class HomeController {
@Autowired
private HomeService homeService;
@RequestMapping("/getHomePage")
public String getHomePage() {
System.out.println("IN HOME");
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public
String login(@RequestBody final UserVO userVO,final ModelMap map) {
String view ="login";
return view;
}
}
我的UserVO是
/**
*
*/
package in.ac.nitk.tas.vo;
import java.io.Serializable;
/**
* @author nitk
*
*/
public class UserVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName;
private String password;
private String userType;
/**
* @return the userType
*/
public String getUserType() {
return userType;
}
/**
* @param userType the userType to set
*/
public void setUserType(String userType) {
this.userType = userType;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
我已经包含了这些JS IN JSP
<script type="text/javascript" src="javascript/jquery-1.10.2.js"></script>
<script type="text/javascript" src="javascript/jquery.json-2.2.js"></script>
<script type="text/javascript" src="javascript/home.js"></script>