我正在开发一个简单的java web应用程序,它允许用户在文本框中输入一些信息,并在提交输入的文本时,在另一个页面(success.jsp)中回显。我在 StackOverflow 中引用了许多教程和问题,但无法完成此任务。你能帮我解决这个问题吗?
我想将 jsp 表单中的文本提交到 SpringMVC Controller 。 Maven 用于依赖管理。
非常感谢
mainController.java
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class mainController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Map<String, Object> model){
textConv textconv = new textConv();
model.put("textcovn", textconv);
return "index";
}
@RequestMapping(value = "/translate", method = RequestMethod.POST)
public String translate(@ModelAttribute("textconv") textConv textconv, Map<String, Object> model){
System.out.println(textconv.getTextFrom());
return "success";
}
}
textConv.java
public class textConv {
String textFrom;
public String getTextFrom() {
return textFrom;
}
public void setTextFrom(String textFrom) {
this.textFrom = textFrom;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<h3>Enter text</h3>
<form action="translate" method="POST" commandName="textconv">
<textarea rows="4" cols="50" path="textFrom">Enter text here</textarea>
<input type="submit" value="Echo">
</form>
</body>
</html>
成功.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration</title>
</head>
<body>
<h3>Text : ${textconv.getTextFrom}</h3>
</body>
</html>
最佳答案
您忘记将请求参数绑定(bind)到模型。将其绑定(bind)到模型并将其传递到成功 View 。将您的 TestController
更改为
@Controller
public class TestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/translate", method = RequestMethod.POST)
public String translate(@RequestParam String textForm, Model model) {
// this will bind the request param to model
model.addAttribute("textForm", textForm);
return "success";
}
}
您还使用了不正确的表格来提交请求。如果您只想使用简单的 HTML 表单,请在 index.jsp
<form action="/translate" method="POST">
<div class="form-group">
<div class="col-md-4">
<textarea class="form-control" name="textForm">
default text
</textarea>
</div>
</div>
<input type="submit" value="Submit">
</form>
当您提交表单并分派(dispatch)到 success.jsp
View 时,使用正确的表达式来打印模型值
<body>
<h3>Text : ${textForm}</h3>
</body>
关于java - 使用 Spring MVC Controller 映射 JSP 文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51009913/