2015-04-21 56 views
0

我有一個非常簡單的Spring MVC項目:如何在春天MVC結果頁上的URL參數?

@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> 
這樣

,成功的網址。 jsp頁面與query.jsp相同:http://localhost:8080/project/form.htm。我需要在query.jsp上添加一些參數,如:http://localhost:8080/project/form.htm?Id=xxx,所以成功頁面可以發送給其他人,結果存在,我該怎麼做?謝謝。
Spring MVC的版本:2.5.6

回答

0

你有用戶模型屬性就像你已經做了successFormqueryForm對象,而不是在URL的末尾發送參數。您可以訪問通過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> 

或者你可以使用RedirectAttri像這樣的012tes

@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 
} 
+0

謝謝,我可以訪問successForm中的屬性,但我怎樣才能使用它們來生成URL的參數? – nzomkxia