input type="hidden" name="userInfoName" value="userName"
value 是由 TokenProcessor 類中的 generateToken() 獲得的,是根據當前使用者的 session 物件和當前時間的 long 值來計算的。protected boolean isTokenValid(javax.servlet.http.HttpServletRequest request)
isTokenValid() 方法判斷儲存在當前使用者對談中的令牌值和請求引數中的令牌值是否相同。如果相同,就返回 true,如果符合以下情況之一,便會返回 false。protected void resetToken(javax.servlet.http.HttpServletRequest request)
resetToken() 方法從當前 session 對談中刪除令牌屬性。protected void saveToken(javax.servlet.http.HttpServletRequest request)
saveToken() 方法用來建立一個新的令牌,並把它儲存在當前 session 範圍內。如果 HttpSession 物件不存在,就首先建立一個 HttpSession 物件。public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) saveToken(request); //前面的處理省略 return mapping.findForward("add"); }
public ActionForward insert(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) if(isTokenValid(request,true)){ //表單不是重複提交//這裡是儲存資料的程式碼 }else{ //表單重複提交 s aveToken(request); //其他的處理程式碼 } }
public class PrepareInsertAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response){ saveToken(request); //建立一個新令牌 return mapping.findForward("prepareInsertAction"); } }
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html:form action="userInfoAction.do"> <table width="281" height="102" border="1"> <tr> <td width="73" height="26" bgcolor="#000000"><div align="center" class= "word_white">姓名</div></td> <td width="192"><div align="center"> <html:text property="name"/><html:errors property="name"/>//定義文字方塊表單項 </div></td> </tr> <tr> <td height="32"bgcolor="#000000"><div align="center" class="word_white">年齡 </div></td> <td><div align="center"> <html:text property="age"/><html:errors property="age"/> </div></td> </tr> <tr> <td height="34"bgcolor="#000000"><div align="center" class="word_white">職業 </div></td> <td><div align="center"> <html:text property="profession"/><html:errors property="profession"/> </div></td> </tr> </table> <input type="submit" name="Submit2" value="提交">&nbsp;&nbsp;&nbsp;//提交按鈕 <input type="reset" name="Submit" value="重置">&nbsp;&nbsp;&nbsp;//重置按鈕 <a href="index.jsp">返回</a>//返回超級連結 </html:form>
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value= "a9bf32c5fade032e405947bfe15ea18f">
public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response){ UserInfoForm userInfoForm = (UserInfoForm)form; //獲取與表單對應的ActionForm物件 userInfoForm.setAge(Integer.valueOf(request.getParameter("age"))); //設定ActionForm物件的age屬性 userInfoForm.setName(Chinese.chinese(request.getParameter("name"))); //設定ActionForm物件的name屬性 userInfoForm.setProfession(Chinese.chinese(request.getParameter("profession"))); ActionMessages errors = new ActionMessages() ; //建立ActionMessages物件 if(!isTokenValid(request)){ //判斷session對談中的令牌值和請求引數中的值是否相等 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.invalid.token")); //向ActionMessages物件中新增物件 saveErrors(request,errors); //儲存ActionMessages物件 saveToken(request); //建立新的令牌 request.setAttribute("success","錯誤!"); //將提示資訊儲存在request物件中 }else{ dao.addUserInfo(userInfoForm); //新增使用者資訊的方法 resetToken(request); request.setAttribute("success","新增使用者資訊成功!"); } return mapping.findForward("success"); }
<action-mappings> <action name="userInfoForm" path="/userInfoAction" scope="request" type= "com.action.UserInfoAction" validate="true">//設定Action <forward name="success" path="/success.jsp"/>//請求轉發地址 </action> <action path="/prepareInsertAction" type="com.action.PrepareInsertAction"> <forward name="prepareInsertAction" path="/insert.jsp"/> </action> </action-mappings>