java - How to load values into the same JSP page on button click event -
i have jsp page 1 button "load" , 2 textfields - "weather" , "companions". on click of button should call controller , controller has load values typed in textfields table of same jsp page. how can that? when click on "load", nothing happens.
jsp page
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>test 1</title> </head> <body> <h2>context information</h2> <table> <tr> <td><form:label path="weather">weather</form:label></td> <td><form:input path="weather" /></td> </tr> <tr> <td><form:label path="companions">companions</form:label></td> <td><form:input path="companions" /></td> </tr> </table> <table border="1" cellspacing="1" align="center" style="margin-top: 160px;"> <tr> <th>weather</th> <th>companions</th> </tr> <tr> <td>${weather}</td> <td>${companions}</td> </tr> </table> <button onclick="window.location.href=window.location.href;">load</button> </body> </html>
controller
package com.test1.controller; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.ui.modelmap; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.servlet.modelandview; @controller public class test1 { @requestmapping(value = "/context", method = requestmethod.get) public modelandview context() { return new modelandview("context", "command", new context()); } @requestmapping(value = "/context", method = requestmethod.post) public string loaddata(@modelattribute("springweb")context context, modelmap model) { model.addattribute("weather", context.getweather()); model.addattribute("companions", context.getcompanions()); return "result"; } }
@requestmapping(value = "/loaddata", method = requestmethod.get) public @responsebody string loaddata(@modelattribute("springweb")context context, modelmap model) { /** here assuming context.getweather() , context.getcompanions() returns string or @ least values can convert string using tostring() method. if need convert them string need change code context.getweather().tostring() + context.getcompanions().tostring() **/ return context.getweather() + context.getcompanions(); }
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript"> function loadajaxstring() { $.ajax({ url : '/loaddata.html', success : function(data) { var weather = data.substring(0, endofindexofweather); var companion = data.substring(endofindexofweather); $('#tdweather').val(weather ); $('#tdcompanions').val(companion); } }); } </script>
Comments
Post a Comment