jquery - Ajax not working with spring3 mvc and tiles -
if give url link hits controller method when give same url in ajax not go it.it hits previous method in controller , reloads home page.
my web.xml is
<?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>
my spring-servlet.xml is
<?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>
i calling function on press of login button
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); } }); }
my controller this
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; } }
my uservo is
/** * */ 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 usertype */ public string getusertype() { return usertype; } /** * @param usertype usertype set */ public void setusertype(string usertype) { this.usertype = usertype; } /** * @return username */ public string getusername() { return username; } /** * @param username username set */ public void setusername(string username) { this.username = username; } /** * @return password */ public string getpassword() { return password; } /** * @param password password set */ public void setpassword(string password) { this.password = password; } }
i have included these 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>
Comments
Post a Comment