我是弹簧技术的新手,请帮助解决这个问题。 我正在开发一个春天的Web应用程序,我有一个leaveRecords.jsp页面,它应该加载以下内容,
对于每个记录,页面应使用两个按钮(如
)将记录显示在表中<button id="Accept"
name="action"
type="submit"
value="Accept<%= dtoBean.getEmployee_id()/>
<button id="Reject"
name="action"
type="submit"
value="Accept<%= dtoBean.getEmployee_id()/>
点击“接受”按钮后,将执行相关操作并应重定向到同一页面,但这次页面应包含按钮
<button id="Cancel"
name="action"
type="submit"
value="Accept<%= dtoBean.getEmployee_id()
/&gt;
leaveRecords.jsp 是:
<form:form method="POST" action="upcomingLeaves.do" commandName="loginForm" modelAttribute="loginForm">
<% CommonDTOBean dtoBean=(CommonDTOBean)session.getAttribute("dtoBean");
List upcomingLeavesList=(ArrayList)session.getAttribute("upcomingLeavesList");%>
<table ><col><thead ><tr style="background-color:#666633;">
<th>Employee id</th>
<th>Leave Balance</th>
<th>Date</th>
<th>Leave Type</th>
<th>Leave Period</th>
<th>Applied Leaves</th>
<th>Status</th>
<th colspan="4">Action</th>
</tr></thead>
<tbody>
<%if(upcomingLeavesList!=null){
for(int i=0;i<upcomingLeavesList.size();i++){
dtoBean=(CommonDTOBean)upcomingLeavesList.get(i);%>
<tr>
<td ><span><%= dtoBean.getEmployee_id() %></span></td>
<td ><span><%= dtoBean.getNo_of_leave() %></span></td>
<td ><span></span><%= dtoBean.getFromDate() %>-<%= dtoBean.getToDate() %></td>
<td > <span><%= dtoBean.getLeaveType() %></span></td>
<td ><span></span><%= dtoBean.getLeavePeriod() %></td>
<td><span></span><%= dtoBean.getAppliedLeave() %></td>
<td><span></span><%= dtoBean.getLeaveStatus() %></td>
<td><button id="btnAccept" name="action" type="submit" value="Accept<%= dtoBean.getEmployee_id() %>" onclick="">Approve</button></td>
<td><button id="btnReject" name="action" type="submit" value="Reject<%= dtoBean.getEmployee_id() %>">Reject</button></td>
<td><button id="btnCancel" name="action" type="submit" value="Cancel<%= dtoBean.getEmployee_id() %>">Cancel</button></td>
</tr>
<%}}%>
</tbody>
</table>
</form:form>
控制器类是:
public String ApproveLeaves(@RequestParam(required=false , defaultValue="")String aion,@RequestParam(required=false,defaultValue="")String Cancel,HttpServletRequest request, HttpServletResponse response, ModelMap model){
try{
//following strings are used for getting the value of button and spiting it to get employee id
String buttonName=request.getParameter("action");
String buttonValue=buttonName.substring(0,6);// here we are spliting up the string and button name
int buttonValue1=Integer.parseInt(buttonName.substring(6));
if (buttonValue.equalsIgnoreCase("Reject"))
{
boolean status=LeaveStatusWorker.Approve(buttonValue1,buttonValue,dtoBean);
if (status)
{
return "redirect: GlobalConstants.UPCOMING_LEAVES";
}
}
if (buttonValue.equalsIgnoreCase("Cancel"))
{
boolean status=LeaveStatusWorker.Approve(buttonValue1,buttonValue,dtoBean);
if (status)
{
return "redirect: GlobalConstants.UPCOMING_LEAVES";
}
}
if (buttonValue.equalsIgnoreCase("Accept"))
{
boolean status=LeaveStatusWorker.Approve(buttonValue1,buttonValue,dtoBean);
if (status)
{
return "redirect: GlobalConstants.UPCOMING_LEAVES";
}
}
return GlobalConstants.UPCOMING_LEAVES;
}
catch(Exception e)
{
System.out.println(e);
}
return GlobalConstants.ERRORPAGE;
}
服务类有一个db交互方法:
public static boolean Approve(int id,String buttonValue,CommonDTOBean dtoBean2)
{
try {
con=DBConnection.getConnection();
String query="select no_of_leave from newemp_register where emp_id=?";//get available leaves from db
pstmt.executeQuery();
if(buttonValue.equalsIgnoreCase("Cancel"))
{
String approve="Update newemp_register set no_of_leave=? where emp_id=?";
pstmt.executeUpdate();
}
if(buttonValue.equalsIgnoreCase("Reject"))
{
return true;
}
if(buttonValue.equalsIgnoreCase("Accept"))
{
String approve="Update newemp_register set no_of_leave=? where emp_id=?";
pstmt.executeUpdate();
return true;
}
}
}//End Of while
} catch (Exception e) {
}
return false;
}//End of Approve
请仔细阅读这个示例,并帮助我,我已经非常努力地在谷歌上搜索但无法解决它。
CommonDTOBean类:
public class CommonDTOBean {
private int emp_id;
private String Status;
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
答案 0 :(得分:0)
使用JSTL条件呈现jsp中的DOM。像:
<form:form method="POST" action="upcomingLeaves.do" commandName="loginForm" modelAttribute="loginForm">
<table>
<thead>
<tr style="background-color:#666633;">
<th>Employee id</th>
<th>Leave Balance</th>
<th>Date</th>
<th>Leave Type</th>
<th>Leave Period</th>
<th>Applied Leaves</th>
<th>Status</th>
<th colspan="4">Action</th>
</tr></thead>
<tbody>
<c:choose>
<c:when test="${not empty upcomingLeavesList}">
<c:forEach items="${upcomingLeavesList}" var="upComLeave">
<tr>
<td><span>${upComLeave.emp_id}</span></td>
<td><span>${upComLeave.status}</span></td>
<td><span>${upComLeave.fromDate} - ${upComLeave.toDate}</span></td>
<td><span>${upComLeave.leaveType}</span></td>
<td><span>${upComLeave.leavePeriod}</span></td>
<td><span>${upComLeave.appliedLeave}</span></td>
<td><span>${upComLeave.leaveStatus}</span></td>
<c:if test="${upComLeave.leaveStatus ne 'accepted' }"> //check status if accepted, don't render Accept button
<td><button id="btnAccept" name="action" type="submit" value="Accept${upComLeave.emp_id}" onclick="">Approve</button></td>
</c:if>
<c:if test="${upComLeave.leaveStatus ne 'rejected' }">//check status if accepted, don't render Reject button
<td><button id="btnReject" name="action" type="submit" value="Reject${upComLeave.emp_id}">Reject</button></td>
</c:if>
<c:if test="${upComLeave.leaveStatus eq 'cancel' }">//check status if cancel, render cancel button
<td><button id="btnCancel" name="action" type="submit" value="Cancel${upComLeave.emp_id}">Cancel</button></td>
</c:if>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
upcomingLeavesList is empty or null..
</c:otherwise>
</c:choose>
</tbody>
</table>
</form:form>
编辑1(基于评论):
如果我点击批准页面应重定向只有取消和 拒绝按钮
如果条件如下,请再使用一次:
<c:if test="${upComLeave.leaveStatus eq 'accepted' }">
<td><button id="btnReject" name="action" type="submit" value="Reject${upComLeave.emp_id}">Reject</button></td>
<td><button id="btnCancel" name="action" type="submit" value="Cancel${upComLeave.emp_id}">Cancel</button></td>
</c:if>
注意:
在jsp:
之上添加它<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>