如何将几个参数从一个视图传递到另一个视图(Spring MVC)

时间:2013-12-20 02:55:56

标签: java jsp spring-mvc parameter-passing

将这些参数从一个jsp页面传递到另一个页面的最佳方法是什么?

第一个处理程序方法:

@RequestMapping(value="/search", method = RequestMethod.POST)
public String postSearchStudent(
                    @RequestParam(value = "program.programId") String programId,
                    @RequestParam(value = "rollNumber") String rollNumber,
                    @RequestParam(value = "rollYear") String rollYear,
                    @RequestParam(value = "indexNumber") String indexNumber,
                    Model model) {

    // do something ???

    // Call StudentService to do the searching
    Student student = studentService.search(indexNumber);

    // if student isn't in the db load addStudent.jsp, else load student.jsp
    if (student == null)
        return "addStudent";
    else
        return "student";
}

searchStudent.jsp:

<c:url var="searchUrl" value="/essays/main/student/search" />
<form:form modelAttribute="student" method="POST" action="${searchUrl}">

第二个处理程序方法:

@RequestMapping(value="/add", method = RequestMethod.GET)
public String getAddStudent(
                    @RequestParam(value = "program.programId") String programId,
                    @RequestParam(value = "rollNumber") String rollNumber,
                    @RequestParam(value = "rollYear") String rollYear,
                    @RequestParam(value = "indexNumber") String indexNumber,
                    Model model) {

        Student student = new Student();
        student.setRollNumber(rollNumber);
        student.setRollYear(rollYear);
        student.setIndexNumber(indexNumber);
        Program program = programService.get(programId);
        student.setProgram(program);
        model.addAttribute("student", student);

        return "addStudent";
}

addStudent.jsp:

<c:url var="saveUrl" value="/essays/main/student/add" />
<form:form commandName="student" method="POST" action="${saveUrl}">

有谁能告诉我如何将这4个参数(programId,rollNumber,rollYear,indexNumber)从searchStudent.jsp传递给addStudent.jsp?

3 个答案:

答案 0 :(得分:1)

为什么不将这些保存在Cookie中?使用像jquery cookie插件这样的东西?或者将这些参数作为属性添加到模型对象中,并将模型传递给视图,或者如果您使用的是Spring 3.2,请查看@ ControllerAdvice之类的内容。很多选择。

答案 1 :(得分:0)

查看请求调度程序。您需要将请求转发到addStudent.jsp。

http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html

答案 2 :(得分:0)

最好的方法是为此属性创建一个表单

StudentCriteriaForm

public class StudentCriteriaForm {

    private String programId;
    private String rollNumber;
    private String rollYear;
    private String indexNumber;

    //setter && Getter Methods
}

现在将此属性绑定到您的jsp,如下所示

searchStudent.jsp

<c:url var="searchUrl" value="/essays/main/student/search" />
<form:form modelAttribute="student" method="POST" action="${searchUrl}" commandName="studentCriteriaForm">
    <form:input path="programId" />
    <form:input path="rollNumber" />
    <form:input path="rollYear" />
    <form:input path="indexNumber" />
    <input type="submit" value="Search" id="btnSearch" >
</form:form>

控制器

@RequestMapping(value="/search", method = RequestMethod.POST)
public String postSearchStudent(@ModelAttribute("studentCriteriaForm") StudentCriteriaForm studentCriteriaForm,
    ModelMap model,HttpServletRequest request) {
    // do something ???
    // Call StudentService to do the searching
    Student student = studentService.search(studentCriteriaForm.getIndexNumber());
    // if student isn't in the db load addStudent.jsp, else load student.jsp
    if (student == null) {
    //here you can pass your parameters to your addStudent.jsp
        model.addAttribute("studentCriteriaForm", studentCriteriaForm);
    return "addStudent";
    } else {
        return "student";
    }   
}

现在您可以在 addStudent.jsp 页面上获取此参数的值

<c:url var="saveUrl" value="/essays/main/student/add" />
<form:form commandName="student" method="POST" action="${saveUrl}">
<c:out value="${studentCriteriaForm.programId}"></c:out>
<c:out value="${studentCriteriaForm.rollNumber}"></c:out>
<c:out value="${studentCriteriaForm.rollYear}"></c:out>
<c:out value="${studentCriteriaForm.indexNumber}"></c:out>

您也可以直接从postSearchStudent()方法导航到getAddStudent()方法。 希望这能解决你的问题。