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

标签 java spring jsp

我有一个 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/

相关文章:

java - 异常: NumberFormatException: For input string in JSP list Page

java - 清除文本文件内容的更干净的方法

java - 如何将此代码片段变形为逻辑取决于索引值的 Java 8?

java - Spring Boot : add new yml files to application config

java - 如何在某个 Spring 配置文件中禁用飞路?

java - 如何防止在JSP页面或HTML中删除当前登录用户

java - 如何获取 setOnItemSelectedListener 之外的变量的更新值?

java - 如何调用以 SelectEvnet 作为参数的方法?

java.net.UnknownHostException : Test: Test: unknown error Failed to get local InetAddress for VMID

java - 如何对 JSONArray 进行排序?