gpt4 book ai didi

java - 如何在按钮单击事件上将值加载到同一 JSP 页面中

转载 作者:行者123 更新时间:2023-12-01 18:05:04 25 4
gpt4 key购买 nike

我有一个 JSP 页面,其中有一个按钮“加载”和两个文本字段 - “天气”和“同伴”。单击按钮时,它应该调用 Controller ,并且 Controller 必须将文本字段中键入的值加载到同一 JSP 页面的表中。我怎样才能做到这一点?当我点击“加载”时,没有任何反应。

JSP页面

<%@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 i am assuming context.getWeather() and context.getCompanions() returns string or at least values you can convert to string using toString() method. If you need to convert them to string then you will need to change the code to 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>

关于java - 如何在按钮单击事件上将值加载到同一 JSP 页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com