J2EE中的struts2表單細(xì)節(jié)處理
/struts-tags中自帶了很多標(biāo)簽
比如一個簡單的登錄表單,其中自帶了很多的樣式,實際上如果你不需要用到struts的實際功能的時候不建議使用
<s:form action="user_save"> <s:token></s:token> <s:textfield name="username" label="用戶名"></s:textfield> <s:textfield name="pwd" label="密碼"></s:textfield> <s:submit value="提交"></s:submit> </s:form>
你可以通過設(shè)置屬性 theme="simple"來取消他自帶的樣式
其次是ModelDriven,意思是直接把實體類當(dāng)成頁面數(shù)據(jù)的收集對象。在Action實現(xiàn)ModelDriven接口,可以很方便的對實體類對象的屬性賦值,不過在Action中實體類對象要new出來并且重寫ModelDriven的getModel方法,返回值是你的實體類對象代碼如下:
package com.xinzhi.action; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.opensymphony.xwork2.util.ValueStack; import com.xinzhi.dao.impl.UserDaoImpl; import com.xinzhi.entity.UserEntity; public class UserAction extends ActionSupport implements ModelDriven<UserEntity> { private static final long serialVersionUID = 1L; private UserEntity userEntity = new UserEntity(); UserDaoImpl userDaoImpl = new UserDaoImpl(); public UserEntity getUserEntity() { return userEntity; } public void setUserEntity(UserEntity userEntity) { this.userEntity = userEntity; } public UserEntity getModel() { return userEntity; } }
然后是表單的數(shù)據(jù)回顯,在Action當(dāng)中把你的實體類對象壓入(ValueStack)堆棧中,然后在頁面中取出堆棧你要的值,方法如下
public String view() { UserEntity selectAUserEntity = userDaoImpl.selectAUserEntity(userEntity .getId()); ValueStack valueStack = ActionContext.getContext().getValueStack(); valueStack.pop(); valueStack.push(selectAUserEntity); return "view"; }
最后是防止表單重復(fù)提交的方法token,我對他的理解是,在表單中如果有<token>標(biāo)簽的時候,提交表單的同時在表單頁和action中隨機(jī)生成一個相同的ID值,當(dāng)?shù)谝淮翁峤贿^來的表單被接收時這個ID將被刪除,當(dāng)被重復(fù)提交時就會找不到對應(yīng)的ID值導(dǎo)致無法重復(fù)提交,并且發(fā)出無效指令的錯誤代碼如下
表單代碼
<s:form action="user_save"> <s:token></s:token> <s:textfield name="username" label="用戶名"></s:textfield> <s:textfield name="pwd" label="密碼"></s:textfield> <s:submit value="提交"></s:submit> </s:form>
然后要在struts.xml配置文件中使用對應(yīng)的攔截器,并指出重復(fù)提交時,無效的指令將會跳轉(zhuǎn)到哪一個頁面代碼如下:
<action name="user_*" class="com.xinzhi.action.UserAction" method="{1}"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="token"> <param name="includeMethods">save</param> </interceptor-ref> </action>
以上所述是小編給大家介紹的J2EE中的struts2表單細(xì)節(jié)處理,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Java PriorityQueue優(yōu)點和缺點面試精講
這篇文章主要為大家介紹了Java面試中PriorityQueue的優(yōu)點和缺點及使用注意詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10SpringBoot中通過AOP整合日志文件的實現(xiàn)
本文主要介紹了SpringBoot中通過AOP整合日志文件的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12