我有一个非常简单的spring mvc项目:
@Controller
@RequestMapping("/form.htm")
public class FormController {
FormValidation formValidation;
@Autowired
public FormController(FormValidation formValidation) {
this.formValidation = formValidation;
}
@RequestMapping(method = RequestMethod.GET)
public String initForm(ModelMap model) {
QueryForm queryForm = new QueryForm();
model.addAttribute("queryForm", queryForm);
return "queryForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(ModelMap model, @ModelAttribute("queryForm")QueryForm queryForm,
BindingResult result) {
List<String> Result = query(queryForm);
SuccessForm successForm = new SuccessForm();
successForm.setId(queryForm.getId());
model.addAttribute("successForm", successForm);
return "successForm";
}
private List<String> query(QueryForm queryForm) {
}
}
success.jsp:
<head>
<title>deviceInfo display</title>
</head>
<body>
<h1>Id is ${successForm.Id}.</h1>
<br>
<c:forEach var="info" items="${successForm.Info}" >
<p style="font-size:120%"><c:out value="${info}" /></p>
<hr />
</c:forEach>
<br>
</body>
</html>
query.jsp:
<head>
<meta charset="utf-8">
</head>
<body>
<div id="main">
<form class="form" id="queryForm" method="POST" command="queryForm">
<fieldset>
<legend>Please fill in the form</legend>
<p>
<label for="Id" class="sr-only">Id</label>
<input type="text" name="Id" id="Id" class="form-control" placeholder="Id">
</p>
<p>
<button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</p>
</fieldset>
</form>
</div>
</body>
</html>
以这种方式,success.jsp页面的url与query.jsp:http://localhost:8080/project/form.htm相同。我需要在query.jsp上添加一些参数,例如:http://localhost:8080/project/form.htm?Id=xxx,因此成功页面可以发送给其他结果存在,我该怎么做?谢谢。
spring mvc version: 2.5.6
答案 0 :(得分:0)
您必须使用您已为successForm
和queryForm
对象执行的用户模型属性,而不是在网址末尾发送参数。您可以通过jsp标签访问模型属性,如
<c:out value='${modelObject.attrProperty}' />
示例
<c:out value='${successForm.Id}' /> //for object's properties
<c:out value='${isUserValid}' /> //for boolean values
列表:
List<UserForm> userForms = LoadFromDB();
model.addAttribute("userForms", userForms);
JSTL迭代上面的列表:
<c:forEach items="${userForms}" var="userForm">
// Do something
</c:forEach>
或者您可以像这样使用RedirectAttributes
@RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
...
redirectAttributes.addAttribute("Id", "IdValue");
redirectAttributes.addFlashAttribute("flashVal", flashValue);
return "redirect:/successPage"; //redirects to the `successPage` with params
}