[英]controller for pass jsp input to Rest API and pass the response to another jsp using spring MVC
[英]Map JSP text input with Spring MVC controller
我正在开发一个简单的Java Web应用程序,该应用程序允许用户在文本框中输入一些信息,并在提交输入的文本时在另一个页面(success.jsp)中回显。 我在StackOverflow中已经提到了许多教程和问题,但是无法完成。 你能帮我这个忙吗?
我想将jsp表单中的文本提交到SpringMVC控制器。 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>
success.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>
您忘记了将请求参数绑定到模型。 将其绑定到模型,并将其传递到成功视图。 将您的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>
当您提交表单并将其分派到success.jsp
视图时,请使用正确的表达式来打印模型值
<body>
<h3>Text : ${textForm}</h3>
</body>
我在这里与您分享一个简单易懂且可行的解决方案。
EmployeeController.java
@Controller
public class EmployeeController {
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView showForm() {
return new ModelAndView("employeehome", "employee", new Employee());
}
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute("employee") Employee employee, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "employeeerror";
}
model.addAttribute("name", employee.getName());
model.addAttribute("contactNumber", employee.getContactNumber());
model.addAttribute("id", employee.getId());
return "employeeview";
}
}
Employee.java
public class Employee {
private String name;
private long id;
private String contactNumber;
public Employee() {
super();
}
public Employee(String name, long id, String contactNumber) {
super();
this.name = name;
this.id = id;
this.contactNumber = contactNumber;
}
//getters and setters
}
employeehome.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>Insert title here</title>
</head>
<body>
<h3>Welcome, Enter The Employee Details</h3>
<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee" modelAttribute="employee">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="id">Id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:label path="contactNumber">Contact Number</form:label></td>
<td><form:input path="contactNumber" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
employeeview.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="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>Insert title here</title>
</head>
<body>
<h2>Submitted Employee Information</h2>
<table>
<tr>
<td>Name :</td>
<td>${name}</td>
</tr>
<tr>
<td>ID :</td>
<td>${id}</td>
</tr>
<tr>
<td>Contact Number :</td>
<td>${contactNumber}</td>
</tr>
</table>
</body>
</html>
employeeerror.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<h3>Pleas enter the correct details</h3>
<table>
<tr>
<td><a href="employee">Retry</a></td>
</tr>
</table>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.