sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-01 18:05:04 25 4
mua khóa gpt4 Nike

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

JSP页面

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<đầu>
Test 1


Context information




Weather



Companions




style="margin-top: 160px;">

Weather
Companions


${weather}
${companions}





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";
}

}

1 Câu trả lời

@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();
}
<小时>


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

25 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com