如何在同一表单上的多个按钮上执行多个操作?

时间:2014-03-21 21:00:47

标签: spring jsp

我在表格中显示一些数据,每行有两个按钮。在单击按钮时,应将一些数据传递给控制器​​类,并且应该执行相应的操作。

   @RequestMapping(value = "/upcomingLeaves", method = RequestMethod.GET)
           public String showForm(@ModelAttribute("loginForm")EmpRegistrationForm profileForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model){ 


             LeaveApplyForm leaveapplyform =(LeaveApplyForm)model.get("leaveapplyform");


             try{

                    HttpSession session=super.getSession(model);
                    CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");
                    List upcomingLeavesList=LeaveStatusWorker.upcomingLeaves(dtoBean);
                    session.setAttribute("upcomingLeavesList", upcomingLeavesList);
                    session.setAttribute("dtoBean", dtoBean);
                    model.addAttribute("dtoBean",dtoBean);
                    return GlobalConstants.UPCOMING_LEAVES;
                    }catch(Exception e){

                    }


             return GlobalConstants.UPCOMING_LEAVES;    
           }

        @RequestMapping(value = "/upcomingLeaves" ,method = RequestMethod.POST)
        public String ApproveLeaves(@RequestParam(required=false , defaultValue="")String action,@RequestParam(required=false,defaultValue="")String Cancel,HttpServletRequest request, HttpServletResponse response, ModelMap model){

            try{

            //  System.out.println(loginForm1.getEmployee_id()+"**************************************");
                int id=Integer.parseInt(request.getParameter("emp_id"));

                System.out.println(request.getParameter("emp_id")+"**************************************");
                AdminLoginForm loginForm=(AdminLoginForm)model.get("loginForm");
                if (action.equalsIgnoreCase("Confirm")) {

                    System.out.println("I am not coming here");
                    boolean status=LeaveStatusWorker.Approve(id);
                    if (status) {                   
                return GlobalConstants.UPCOMING_LEAVES;
            }
        }

jsp页面包含每行有两个按钮的表, 1.确定 2.Cancel 单击此按钮时,应该采取适当的操作。如果我想将某些数据从特定行传递给控制器​​,我该如何完成它?请帮帮我。

<form:form method="POST" action="upcomingLeaves.do" commandName="loginForm" modelAttribute="loginForm">
    <table>

            <!-- using Following Statement Take an Record from Leave Table that table present Employee Resent Applied Leave -->
     <%

        CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");

        List upcomingLeavesList=(ArrayList)session.getAttribute("upcomingLeavesList");
    %>

            <tr><th style="padding:0 12px 0 12px;">Emp ID </th><th style="padding:0 12px 0 12px;">No. Of Days</th><th style="padding:0 12px 0 12px;">from Date</th>
            <th style="padding:0 12px 0 12px;">To Date</th><th style="padding:0 12px 0 12px;">Leaves Type</th>
            <th style="padding:0 12px 0 12px;">Leaves Period</th><th style="padding:0 12px 0 12px;">Available Leaves</th><th style="padding:0 12px 0 12px;">Status</th></tr>

            <%
        if(upcomingLeavesList!=null){
        for(int i=0;i<upcomingLeavesList.size();i++){

            dtoBean=(CommonDTOBean)upcomingLeavesList.get(i);%>


            <tr><td ><%=dtoBean.getEmployee_id() %></td><td ><%=dtoBean.getAppliedLeave()%></td>
            <td ><%=dtoBean.getFromDate()%></td><td><%=dtoBean.getToDate()%></td><td ><%=dtoBean.getLeaveType()%></td><td><%=dtoBean.getLeavePeriod()%></td><td ><%=dtoBean.getNo_of_leave()%></td><td ></td>
            <td ><%=dtoBean.getLeaveStatus()%></td><td><input type="submit" id="btn" name="Confirm" value="Confirm"/></td><td><input type="submit" id="btn" name="Cancel" value="Cancel"/></td></tr>
            <tr></tr>

            <tr><td><%}}%></td></tr>

        </table>

1 个答案:

答案 0 :(得分:0)

如果你给你的按钮名称,你就可以知道哪一个被点击了,因为点击了一个,只点击了一个就像参数一样传递:

<input type='submit' name='button1' value='whatever' />
<input type='submit' name='button2' value='whatever' />

在servlet中:

String button1 = request.getParameter("button1");
String button2 = request.getParameter("button2");
if(button1!=null)
{
   //button1 was clicked
}
else if(button2!=null)
{
   //button2 was clicked
}