J2EE中的struts2表單細(xì)節(jié)處理
/struts-tags中自帶了很多標(biāo)簽
比如一個(gè)簡(jiǎn)單的登錄表單,其中自帶了很多的樣式,實(shí)際上如果你不需要用到struts的實(shí)際功能的時(shí)候不建議使用
<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,意思是直接把實(shí)體類當(dāng)成頁面數(shù)據(jù)的收集對(duì)象。在Action實(shí)現(xiàn)ModelDriven接口,可以很方便的對(duì)實(shí)體類對(duì)象的屬性賦值,不過在Action中實(shí)體類對(duì)象要new出來并且重寫ModelDriven的getModel方法,返回值是你的實(shí)體類對(duì)象代碼如下:
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)中把你的實(shí)體類對(duì)象壓入(ValueStack)堆棧中,然后在頁面中取出堆棧你要的值,方法如下
public String view() { UserEntity selectAUserEntity = userDaoImpl.selectAUserEntity(userEntity .getId()); ValueStack valueStack = ActionContext.getContext().getValueStack(); valueStack.pop(); valueStack.push(selectAUserEntity); return "view"; }
最后是防止表單重復(fù)提交的方法token,我對(duì)他的理解是,在表單中如果有<token>標(biāo)簽的時(shí)候,提交表單的同時(shí)在表單頁和action中隨機(jī)生成一個(gè)相同的ID值,當(dāng)?shù)谝淮翁峤贿^來的表單被接收時(shí)這個(gè)ID將被刪除,當(dāng)被重復(fù)提交時(shí)就會(huì)找不到對(duì)應(yīng)的ID值導(dǎo)致無法重復(fù)提交,并且發(fā)出無效指令的錯(cuò)誤代碼如下
表單代碼
<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配置文件中使用對(duì)應(yīng)的攔截器,并指出重復(fù)提交時(shí),無效的指令將會(huì)跳轉(zhuǎn)到哪一個(gè)頁面代碼如下:
<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é)處理,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 簡(jiǎn)單談?wù)凷truts動(dòng)態(tài)表單(DynamicForm)
- Struts2中接收表單數(shù)據(jù)的三種驅(qū)動(dòng)方式
- 詳解struts2的token機(jī)制和cookie來防止表單重復(fù)提交
- Struts2中Action三種接收參數(shù)形式與簡(jiǎn)單的表單驗(yàn)證功能
- Java的Struts框架中登陸功能的實(shí)現(xiàn)和表單處理器的使用
- 通過Ajax兩種方式講解Struts2接收數(shù)組表單的方法
- struts2中一個(gè)表單中提交多個(gè)請(qǐng)求的例子(多個(gè)提交按鈕)
- 基于Struts2實(shí)現(xiàn)防止表單重復(fù)提交
相關(guān)文章
Spring Boot 基于注解的 Redis 緩存使用詳解
本篇文章主要介紹了Spring Boot 基于注解的 Redis 緩存使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Java PriorityQueue優(yōu)點(diǎn)和缺點(diǎn)面試精講
這篇文章主要為大家介紹了Java面試中PriorityQueue的優(yōu)點(diǎn)和缺點(diǎn)及使用注意詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Java 獲取指定日期的實(shí)現(xiàn)方法總結(jié)
以下是對(duì)Java中獲取指定日期的實(shí)現(xiàn)方法進(jìn)行了歸納總結(jié),需要的朋友可以參考下2013-07-07Java實(shí)現(xiàn)兩人五子棋游戲(六) 行棋方變換
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的兩人五子棋游戲,行棋方變換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03SpringBoot中通過AOP整合日志文件的實(shí)現(xiàn)
本文主要介紹了SpringBoot中通過AOP整合日志文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12