2013-12-20 55 views
0

將這些參數從一個jsp頁面傳遞到另一個頁面的最佳方式是什麼?如何將多個參數從一個視圖傳遞到另一個視圖(Spring MVC)

首先處理方法:

@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?

+0

和你在'getAddStudent()'方法中做的一樣... – zeroflagL

回答

1

爲什麼不能在餅乾保存這些?使用類似jquery cookie插件的東西?或者將這些參數作爲屬性添加到模型對象中,並將模型傳遞給視圖,或者如果您使用的是Spring 3.2,則可以查看類似@ ControllerAdvice的內容。有很多選擇。

0

最好的方法是創建一個表單這個屬性作爲

StudentCriteriaForm

public class StudentCriteriaForm { 

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

    //setter && Getter Methods 
} 

現在結合如下

searchStudent.jsp這個屬性,你的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()方法也。希望這能解決你的問題。

相關問題