當咱們學習Session的時候已經經過Session來編寫了一個防止表單重複提交的小程序了,咱們來回顧一下咱們當時是怎麼作的:html
咱們之前寫表達重複提交就花了這麼幾個步驟…若是有興趣的同窗能夠看一下之前的實現思路:http://blog.csdn.net/hon_3y/article/details/54799494#t11java
Struts2是簡化咱們的開發的,表單重複提交也是一件很是經常使用的功能…Struts2也爲咱們實現了…固然啦,也是經過攔截器來實現web
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
它的實現原理和咱們之前寫的思路幾乎一致…它不須要另外寫一個組件來生成token值,struts2標籤就有這麼一個功能…所以是十分方便的sql
爲了熟悉一下Struts2,咱們也使用Struts2來編寫一下上圖的程序…數據庫
package zhongfucheng.dao; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import zhongfucheng.entity.User; import zhongfucheng.utils.Utils2DB; import java.sql.SQLException; import java.util.List; /** * Created by ozc on 2017/5/3. */ public class UserDao { public void add(User user) { try { String sql = "INSERT INTO user(id,username,cellphone,password,address) VALUES (?,?,?,?,?)"; QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); queryRunner.update(sql, new Object[]{user.getId(), user.getUsername(), user.getCellphone(), user.getPassword(),user.getAddress()}); } catch (SQLException e) { new RuntimeException("登錄失敗了!"); } } public User findUser(String id) { try { String sql = "SELECT * FROM user WHERE id=?"; QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); return (User) queryRunner.query(sql, new BeanHandler(User.class), new Object[]{id}); } catch (SQLException e) { new RuntimeException("登錄失敗了!"); } return null; } public List<User> getAll() { try { String sql = "SELECT * FROM user"; QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); return (List<User>) queryRunner.query(sql, new BeanListHandler(User.class)); } catch (SQLException e) { new RuntimeException("登錄失敗了!"); } return null; } public void updateUser(User user) { try { String sql = "UPDATE user SET username=?,password=?,cellphone=? WHERE id=?"; QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); queryRunner.update(sql, new Object[]{user.getUsername(), user.getPassword(), user.getCellphone(), user.getId()}); } catch (SQLException e) { new RuntimeException("登錄失敗了!"); } } }
package zhongfucheng.service; import zhongfucheng.dao.UserDao; import zhongfucheng.entity.User; import zhongfucheng.utils.WebUtils; import java.util.List; /** * Created by ozc on 2017/5/3. */ public class Service { UserDao userDao = new UserDao(); public void add(User user) { //手動設置id,由於在數據庫表我沒使用自動增加id user.setId(WebUtils.makeId()); //這是之前的表,規定要address,只能手動設置了 user.setAddress("廣州"); userDao.add(user); } public User findUser(String id) { return userDao.findUser(id); } public List<User> getAll() { return userDao.getAll(); } public void updateUser(User user) { userDao.updateUser(user); } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <form action="${pageContext.request.contextPath}/user_register" method="post"> <table border="1"> <tr> <td>用戶名:<input type="text" name="username"></td> </tr> <tr> <td> 密碼:<input type="password" name="password"></td> </tr> <tr> <td>電話:<input type="text" name="cellphone"></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html>
//這裏必定要實例化 User user = new User(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public User getModel() { return user; } /*******調用service********/ Service service = new Service(); public String register() throws Exception { service.add(user); //註冊成功,就跳轉到list()方法,list方法就跳轉到查看全部用戶頁面了! return list(); }
<%-- Created by IntelliJ IDEA. User: ozc Date: 2017/5/2 Time: 18:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>列出下載頁面</title> </head> <body> <table border="1" align="center"> <tr> <td>用戶id</td> <td>用戶姓名</td> <td>用戶密碼</td> <td>用戶電話</td> <td>操做</td> </tr> <s:if test="#request.users!=null"> <c:forEach items="${users}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.cellphone}</td> <td><a href="${pageContext.request.contextPath}/user_updatePage?id=${user.id}">修改</a></td> </tr> </c:forEach> </s:if> </table> </body> </html>
public String updatePage() throws Exception { //獲得用戶帶過來的id,根據id查找對象 User user222 = service.findUser(user.getId()); ActionContext.getContext().getValueStack().push(user222); return "updatePage"; }
<form action="${pageContext.request.contextPath}/user_update"> <table border="1"> <tr> <td>用戶名<s:textfield name="username"/></td> </tr> <tr> <td>密碼 <s:textfield name="password" /></td> </tr> <tr> <td>電話<s:textfield name="cellphone"/></td> </tr> <s:hidden name="id"/> <tr> <td><input type="submit" value="修改"></td> </tr> </table> </form>
上面咱們已經完成了大部分的功能了,但當咱們若是提交以後,再刷新頁面,那麼表單的數據就會重複提交…咱們使用Struts2咱們提供的防止表單重複提交的功能把!apache
<table border="1"> <s:token></s:token> <tr> <td>用戶名:<input type="text" name="username"></td> </tr> <tr> <td> 密碼:<input type="password" name="password"></td> </tr> <tr> <td>電話:<input type="text" name="cellphone"></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table>
token攔截器默認是不會啓動的,也就是說:須要咱們手動配置…小程序
當咱們配置攔截器的時候,Struts2默認的攔截器是不會執行的,因此要把Struts2默認的攔截器也寫上markdown
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.ui.theme" value="simple"/> <package name="xxx" extends="struts-default"> <action name="user_*" class="zhongfucheng.action.UserAction" method="{1}"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="token"> <!-- 要攔截的方法! --> <param name="includeMethods">register</param> </interceptor-ref> <!--若是是list,那麼就跳轉到list的JSP頁面--> <result name="list"> /list.jsp</result> <!--請求跳轉到修改頁面--> <result name="updatePage">/update.jsp</result> <!--若是校驗成功,跳轉到login.jsp頁面回顯--> <result name="success">/login.jsp</result> <result name="redirectList" type="redirect">/user_list</result> </action> </package> <include file="config.xml"/> </struts>