struts2框架是一個非常優秀的mvc框架,時至今日已有很多公司採用其作爲表示層的控制轉發工具,我非常喜歡struts2的攔截器特性和一整套的自定義標籤。在這根據個人使用struts2的經驗,與大家分享一些常用的struts2標籤,希望對大家有所幫助。
- 實例場景
- 需求分析
- 實例效果
- 後臺java代碼
- public class UserBean implements Serializable{
- private static final long serialVersionUID = -5808037703808170288L;
- private int userId; //編號
- private String userName; //姓名
- private String password; //密碼
- private Date birthday = new Date(); //生日:格式yyyy-MM-dd,默認爲當前時間
- private int sex; //性別:0男,1女
- private int[] hobby; //愛好,數組
- private int city; //所屬 城市
- getter、setter...
- }
public class UserBean implements Serializable{
private static final long serialVersionUID = -5808037703808170288L;
private int userId; //編號
private String userName; //姓名
private String password; //密碼
private Date birthday = new Date(); //生日:格式yyyy-MM-dd,默認爲當前時間
private int sex; //性別:0男,1女
private int[] hobby; //愛好,數組
private int city; //所屬 城市
getter、setter...
}
- public class CityBean implements Serializable{
- private static final long serialVersionUID = -6562852059776509594L;
- private int cityId;
- private String cityValue;
- public CityBean(int cityId, String cityValue) {
- super();
- this.cityId = cityId;
- this.cityValue = cityValue;
- }
- getter、setter
- }
public class CityBean implements Serializable{
private static final long serialVersionUID = -6562852059776509594L;
private int cityId;
private String cityValue;
public CityBean(int cityId, String cityValue) {
super();
this.cityId = cityId;
this.cityValue = cityValue;
}
getter、setter
}
- public class TagsService {
- /**
- * Function : 獲取城市的集合
- */
- public List<CityBean> getCitys()
- /**
- * Function : 獲取興趣的集合
- */
- public List<HobbyBean> getHobbis()
- /**
- * Function : 獲取性別的集合
- */
- public List<SexBean> getSexs()
- /**
- * Function : 獲取被選中的興趣愛好集合
- */
- public List<HobbyBean> getCheckedHobbies(int hobbies[])
- /**
- * Function : 獲取被選擇的城市集合
- */
- public CityBean getSelectedCity(int cityId)
- }
public class TagsService {
/**
* Function : 獲取城市的集合
*/
public List<CityBean> getCitys()
/**
* Function : 獲取興趣的集合
*/
public List<HobbyBean> getHobbis()
/**
* Function : 獲取性別的集合
*/
public List<SexBean> getSexs()
/**
* Function : 獲取被選中的興趣愛好集合
*/
public List<HobbyBean> getCheckedHobbies(int hobbies[])
/**
* Function : 獲取被選擇的城市集合
*/
public CityBean getSelectedCity(int cityId)
}
- public class TagsAction extends ActionSupport {
- private static final long serialVersionUID = 4361410156958515185L;
- private TagsService tagsService = new TagsService();
- //****formbean*****
- private List<CityBean> lstCityBean;
- private List<HobbyBean> lstHobbyBean;
- private UserBean userBean;
- //*******action method***********
- /**
- * 進入表單填寫頁面
- */
- public String goIndex(){
- userBean = new UserBean();
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- request.setAttribute("lstSexBean", tagsService.getSexs());
- return SUCCESS;
- }
- /**
- * Function : 提交表單
- */
- public String doSubmit(){
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- request.setAttribute("lstHobby", tagsService.getCheckedHobbies(userBean.getHobby()));
- request.setAttribute("cityBean", tagsService.getSelectedCity(userBean.getCity()));
- return SUCCESS;
- }
- /**
- * Function : 驗證表單數據
- */
- public void validateDoSubmit(){
- if(userBean.getCity()<1){
- this.addFieldError("userBean.city", "請選擇城市!");
- return;
- }
- }
- public List<CityBean> getLstCityBean() {
- return tagsService.getCitys();
- }
- public List<HobbyBean> getLstHobbyBean() {
- return tagsService.getHobbis();
- }
- gettter、setter........
- }
public class TagsAction extends ActionSupport {
private static final long serialVersionUID = 4361410156958515185L;
private TagsService tagsService = new TagsService();
//****formbean*****
private List<CityBean> lstCityBean;
private List<HobbyBean> lstHobbyBean;
private UserBean userBean;
//*******action method***********
/**
* 進入表單填寫頁面
*/
public String goIndex(){
userBean = new UserBean();
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
request.setAttribute("lstSexBean", tagsService.getSexs());
return SUCCESS;
}
/**
* Function : 提交表單
*/
public String doSubmit(){
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
request.setAttribute("lstHobby", tagsService.getCheckedHobbies(userBean.getHobby()));
request.setAttribute("cityBean", tagsService.getSelectedCity(userBean.getCity()));
return SUCCESS;
}
/**
* Function : 驗證表單數據
*/
public void validateDoSubmit(){
if(userBean.getCity()<1){
this.addFieldError("userBean.city", "請選擇城市!");
return;
}
}
public List<CityBean> getLstCityBean() {
return tagsService.getCitys();
}
public List<HobbyBean> getLstHobbyBean() {
return tagsService.getHobbis();
}
gettter、setter........
}
- 表單數據填寫頁面代碼分析
- <%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
- <body>
- <h3>Debug標籤</h3>
- <s:debug></s:debug>
- <hr/>
- <h3>表單標籤</h3>
- <form action="<%=root%>/doSubmit.action" method="post">
- <s:fielderror cssStyle="color:red"></s:fielderror>
- <table>
- <tr>
- <td>編號:</td>
- <td><s:textfield name="userBean.userId"/></td>
- </tr>
- <tr>
- <td>姓名:</td>
- <td><s:textfield name="userBean.userName"></s:textfield></td>
- </tr>
- <tr>
- <td>密碼:</td>
- <td><s:password name="userBean.password"></s:password></td>
- </tr>
- <tr>
- <td>生日:</td>
- <td>
- <s:textfield name="userBean.birthday">
- <s:param name="value">
- <s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" />
- </s:param>
- </s:textfield>
- </td>
- </tr>
- <tr>
- <td>性別:</td>
- <td><s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio></td>
- </tr>
- <tr>
- <td>城市:</td>
- <td><s:select name="userBean.city" list="lstCityBean" listKey="cityId" listValue="cityValue" headerKey="0" headerValue="--請選擇--"></s:select></td>
- </tr>
- <tr>
- <td>愛好:</td>
- <td><s:checkboxlist name="userBean.hobby" list="lstHobbyBean" listKey="hobbyId" listValue="hobbyValue"></s:checkboxlist></td>
- </tr>
- <tr>
- <s:hidden></s:hidden>
- <td><s:submit value="提交"/></td>
- <td><s:reset value="重置"/></td>
- </tr>
- </table>
- </form>
- </body>
<body>
<h3>Debug標籤</h3>
<s:debug></s:debug>
<hr/>
<h3>表單標籤</h3>
<form action="<%=root%>/doSubmit.action" method="post">
<s:fielderror cssStyle="color:red"></s:fielderror>
<table>
<tr>
<td>編號:</td>
<td><s:textfield name="userBean.userId"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><s:textfield name="userBean.userName"></s:textfield></td>
</tr>
<tr>
<td>密碼:</td>
<td><s:password name="userBean.password"></s:password></td>
</tr>
<tr>
<td>生日:</td>
<td>
<s:textfield name="userBean.birthday">
<s:param name="value">
<s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" />
</s:param>
</s:textfield>
</td>
</tr>
<tr>
<td>性別:</td>
<td><s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio></td>
</tr>
<tr>
<td>城市:</td>
<td><s:select name="userBean.city" list="lstCityBean" listKey="cityId" listValue="cityValue" headerKey="0" headerValue="--請選擇--"></s:select></td>
</tr>
<tr>
<td>愛好:</td>
<td><s:checkboxlist name="userBean.hobby" list="lstHobbyBean" listKey="hobbyId" listValue="hobbyValue"></s:checkboxlist></td>
</tr>
<tr>
<s:hidden></s:hidden>
<td><s:submit value="提交"/></td>
<td><s:reset value="重置"/></td>
</tr>
</table>
</form>
</body>
- <s:textfield name="userBean.birthday">
- <s:param name="value">
- <s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" />
- </s:param>
- </s:textfield>
<s:textfield name="userBean.birthday">
<s:param name="value">
<s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" />
</s:param>
</s:textfield>
- <s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio>
<s:radio name="userBean.sex" list="#request.lstSexBean" listKey="sexId" listValue="sexValue"></s:radio>
- request.setAttribute("lstSexBean", tagsService.getSexs());
request.setAttribute("lstSexBean", tagsService.getSexs());
- private List<SexBean> lstSexBean;
- public List<SexBean> getLstSexBean(){
- return tagsService.getSexs();
- }
private List<SexBean> lstSexBean;
public List<SexBean> getLstSexBean(){
return tagsService.getSexs();
}
- <input type="radio" name="userBean.sex" id="userBean_sex0" checked="checked" value="0"/><label for="userBean_sex0">男</label>
- <input type="radio" name="userBean.sex" id="userBean_sex1" value="1"/><label for="userBean_sex1">女</label>
<input type="radio" name="userBean.sex" id="userBean_sex0" checked="checked" value="0"/><label for="userBean_sex0">男</label>
<input type="radio" name="userBean.sex" id="userBean_sex1" value="1"/><label for="userBean_sex1">女</label>
- <select name="userBean.city" id="userBean_city">
- <option value="0"
- selected="selected"
- >--請選擇--</option>
- <option value="1">北京</option>
- <option value="2">上海</option>
- <option value="3">廣州</option>
- <option value="4">成都</option>
- <option value="5">深圳</option>
- </select>
<select name="userBean.city" id="userBean_city">
<option value="0"
selected="selected"
>--請選擇--</option>
<option value="1">北京</option>
<option value="2">上海</option>
<option value="3">廣州</option>
<option value="4">成都</option>
<option value="5">深圳</option>
</select>
<s:checkboxlist>標籤:複選標籤,該標籤的使用方法跟<s:radio>標籤完全類似- <input type="checkbox" name="userBean.hobby" value="1" id="userBean.hobby-1"/>
- <label for="userBean.hobby-1" class="checkboxLabel">唱歌</label>
- <input type="checkbox" name="userBean.hobby" value="2" id="userBean.hobby-2"/>
- <label for="userBean.hobby-2" class="checkboxLabel">跳舞</label>
- <input type="checkbox" name="userBean.hobby" value="3" id="userBean.hobby-3"/>
- <label for="userBean.hobby-3" class="checkboxLabel">運動</label>
- <input type="checkbox" name="userBean.hobby" value="4" id="userBean.hobby-4"/>
- <label for="userBean.hobby-4" class="checkboxLabel">旅遊</label>
- <input type="checkbox" name="userBean.hobby" value="5" id="userBean.hobby-5"/>
- <label for="userBean.hobby-5" class="checkboxLabel">宅神</label>
- <input type="hidden" id="__multiselect_userBean_hobby" name="__multiselect_userBean.hobby" value="" />
<input type="checkbox" name="userBean.hobby" value="1" id="userBean.hobby-1"/>
<label for="userBean.hobby-1" class="checkboxLabel">唱歌</label>
<input type="checkbox" name="userBean.hobby" value="2" id="userBean.hobby-2"/>
<label for="userBean.hobby-2" class="checkboxLabel">跳舞</label>
<input type="checkbox" name="userBean.hobby" value="3" id="userBean.hobby-3"/>
<label for="userBean.hobby-3" class="checkboxLabel">運動</label>
<input type="checkbox" name="userBean.hobby" value="4" id="userBean.hobby-4"/>
<label for="userBean.hobby-4" class="checkboxLabel">旅遊</label>
<input type="checkbox" name="userBean.hobby" value="5" id="userBean.hobby-5"/>
<label for="userBean.hobby-5" class="checkboxLabel">宅神</label>
<input type="hidden" id="__multiselect_userBean_hobby" name="__multiselect_userBean.hobby" value="" />
<s:hidden>標籤:隱藏標籤,可以設置變量值,但是不在頁面顯示- 提交後顯示的頁面
- <body>
- <table>
- <tr>
- <td>編號:</td>
- <td><s:property value="userBean.userId"></s:property></td>
- </tr>
- <tr>
- <td>姓名:</td>
- <td><s:property value="userBean.userName"></s:property></td>
- </tr>
- <tr>
- <td>密碼:</td>
- <td><s:property value="userBean.password"></s:property></td>
- </tr>
- <tr>
- <td>生日:</td>
- <td><s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" /></td>
- </tr>
- <tr>
- <td>性別:</td>
- <td>
- <s:if test="userBean.sex==0">
- 男
- </s:if>
- <s:else>
- 女
- </s:else>
- </td>
- </tr>
- <tr>
- <td>城市:</td>
- <td>
- <s:property value="#request.cityBean.cityValue"/>
- </td>
- </tr>
- <tr>
- <td>愛好:</td>
- <td>
- <s:if test="#request.lstHobby!=null">
- <s:iterator value="#request.lstHobby" var="hobby" status="index" begin="0" end="#request.lstHobby.length-1">
- 第[<s:property value="%{#attr.index.index+1}"/>]條愛好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
- </s:iterator>
- </s:if>
- </td>
- </tr>
- </table>
- </body>
<body>
<table>
<tr>
<td>編號:</td>
<td><s:property value="userBean.userId"></s:property></td>
</tr>
<tr>
<td>姓名:</td>
<td><s:property value="userBean.userName"></s:property></td>
</tr>
<tr>
<td>密碼:</td>
<td><s:property value="userBean.password"></s:property></td>
</tr>
<tr>
<td>生日:</td>
<td><s:date name="userBean.birthday" format="yyyy-MM-dd hh:MM:ss" /></td>
</tr>
<tr>
<td>性別:</td>
<td>
<s:if test="userBean.sex==0">
男
</s:if>
<s:else>
女
</s:else>
</td>
</tr>
<tr>
<td>城市:</td>
<td>
<s:property value="#request.cityBean.cityValue"/>
</td>
</tr>
<tr>
<td>愛好:</td>
<td>
<s:if test="#request.lstHobby!=null">
<s:iterator value="#request.lstHobby" var="hobby" status="index" begin="0" end="#request.lstHobby.length-1">
第[<s:property value="%{#attr.index.index+1}"/>]條愛好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
</s:iterator>
</s:if>
</td>
</tr>
</table>
</body>
- <SPAN style="WHITE-SPACE: pre"> </SPAN>第[<s:property value="%{#attr.index.index+1}"/>]條愛好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
- </s:iterator>
<s:iterator value="#request.lstHobby" var="hobby" status="index" begin="0" end="#request.lstHobby.length-1">
第[<s:property value="%{#attr.index.index+1}"/>]條愛好:<s:property value="%{#attr.hobby.hobbyValue}"/><br/>
</s:iterator>
- OGNL簡介
之所以命名爲OGNL,就是因爲它處理對象很給力,struts能夠將對象層層解析,把各個對象的關係以圖的樣式展示出來。比如userBean.userId,之所以能找到這個對象,就是因爲OGNL會先找userBean對象,然後再找userBean對象裏的userId屬性。假設UserBean這個類還包含了名爲Role的javabean的實例,Role裏面包含字段roleName,我們要找到roleName就可以直接寫<s:property value="user.role.roleName">,OGNL通過對象逐級導航找到子對象。