Struts2學習筆記(4)-----Struts2常見的表單標籤用法

話說學習Struts2的標籤其實早就完成了,只是學習的過程當中出現了一個小意外,那就是將jsp頁面的值傳到Java中時卻老是亂碼,按照全部可行的方法都試過了,惋惜,結果仍是亂碼、亂碼、亂碼。就在我走投無路時發現網上說Struts2 的有個版本有個bug,那就是中文亂碼。因而我將我使用的Struts2的jar包版本換爲2.2.1.1,沒想到真的解決了這個問題。Apache發佈了那麼多Struts2的版本,恰恰讓我撞見了這個有bug的版本,這運氣….鬱悶死我了…css


         好了,言歸正傳。Struts2的UI標籤是很簡單也很容易使用。你只須要在jsp頁面引入<%@ taglib  prefix=」s」  uri=」/struts-tags」%>就能夠直接使用Struts的標籤了。你不用寫任何html代碼。固然你要是不習慣Struts2的標籤樣式,你能夠設置「theme=simple」來取消它,而後你就能夠按照本身的須要去設置樣式了。html


本次學習依然在前幾回的項目基礎上練習的,只是將Struts2的jar包換爲了2.2.1.1的。java


        好了,請看個人這個註冊表單register.jsp,代碼以下:web

<%@ page language="java"contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>註冊表單</title>
    <style type="text/css">
       table.demo{border-collapse: collapse;}
       table.demo th,td {padding: 0; border: 1px solid #000;}
    </style>
</head>
<body>

    <s:form action="register" theme="simple">
       <table  class="demo"  width="400px"height="300px">
           <thead>
              <tr >
                  <th colspan="2"align="center">註冊表</th>
              </tr>
           </thead>
           <tbody>   
              <tr>
                  <td style="text-align: right;">姓名:</td>
                  <td><s:textfield name="username"></s:textfield></td>   
              </tr>
              <tr>
                  <td style="text-align: right;">密碼:</td>
                  <td><s:password name="password"></s:password></td>
              </tr>
              <tr>
                  <td style="text-align: right;">性別:</td>
                  <td><s:radio name="sex"list="sexList" ></s:radio></td>
              </tr> 
              <tr>
                  <td style="text-align: right;">國家:</td>
                  <td><s:select list="countryList"listKey="id" listValue="countryName"name="country" label="國家"></s:select></td>
              </tr>
              <tr>  
                  <td style="text-align: right;">自我介紹:</td>
                  <td><s:textarea name="about"label="自我介紹"></s:textarea></td>
              </tr>
              <tr>
                  <td style="text-align: right;">所屬方向:</td>
                  <td><s:checkboxlist list="communityList" name="community" ></s:checkboxlist></td>
              </tr>
              <tr>
                  <td style="text-align: right;">您是否願意接受簽署協議:</td>
                  <td><s:checkbox name="mailingList"value="贊成"></s:checkbox></td>
              </tr>
              <tr>
                  <td colspan="2"align="center"><s:submit value="提交"></s:submit></td>
              </tr> 
           </tbody>     
       </table>
    </s:form>

</body>
</html>服務器

 看以看出,我爲了按照本身的樣式控制表格,我選擇取消它的樣式。jsp


     更重要的是咱們要理解Struts2的值棧。在Struts 2中,值棧是存儲相關處理請求數據的地方 。所以,全部的form表單屬性都將存儲在值棧 。UI標籤就是經過name屬性來鏈接值棧。因此,name屬性是必填的。ide


     你會發現這個form表單中的radio,select,checkboxlist都有一個list屬性。list能夠是Collection,Map或者Iterator。Select相對來講要複雜不少,listValue是呈如今頁面的值,listKey是與listValue相對應且傳到後臺接受的值。Checkbox返回的是一個boolean值。學習


    關於標籤就介紹這麼多。如今來看看action文件,個人是RegisterAction.java,代碼以下:ui

package com.iman.action;

import java.util.ArrayList;
import java.util.List;

import com.iman.model.Country;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

         private Stringusername;
         private Stringpassword;
         private String sex;
         private Stringcountry;
         private String about;
         private Stringcommunity;
         private booleanmailingList;
         private List<Country>countryList;
         privateList<String> communityList;
         privateList<String> sexList;

         public Stringregister(){
                   returnSUCCESS;
         }
         public StringgetList(){
                   countryList=newArrayList<Country>();
                   countryList.add(newCountry(1,"中國"));
                   countryList.add(newCountry(2,"美國"));
                   countryList.add(newCountry(3,"法國"));
                   countryList.add(newCountry(4,"英國"));

                   communityList=newArrayList<String>();
                   communityList.add("Java");
                   communityList.add(".Net");
                   communityList.add("PHP");

                   sexList=newArrayList<String>();
                   sexList.add("男");
                   sexList.add("女");

                   return"list";
         }
         public StringgetUsername() {
                   returnusername;
         }
         public voidsetUsername(String username) {
                   this.username= username;
         }
         public StringgetPassword() {
                   returnpassword;
         }
         public voidsetPassword(String password) {
                   this.password= password;
         }
         public String getSex(){
                   return sex;
         }
         public voidsetSex(String sex) {
                   this.sex =sex;
         }
         public StringgetCountry() {
                   return country;
         }
         public voidsetCountry(String country) {
                   this.country= country;
         }
         public StringgetAbout() {
                   returnabout;
         }
         public voidsetAbout(String about) {
                   this.about =about;
         }
         public StringgetCommunity() {
                   returncommunity;
         }
         public void setCommunity(Stringcommunity) {
                   this.community= community;
         }
         public booleanisMailingList() {
                   returnmailingList;
         }
         public voidsetMailingList(boolean mailingList) {
                   this.mailingList= mailingList;
         }
         publicList<Country> getCountryList() {
                   returncountryList;
         }
         public voidsetCountryList(List<Country> countryList) {
                   this.countryList= countryList;
         }
         publicList<String> getCommunityList() {
                   returncommunityList;
         }
         public voidsetCommunityList(List<String> communityList) {
                   this.communityList= communityList;
         }
         publicList<String> getSexList() {
                   returnsexList;
         }
         public voidsetSexList(List<String> sexList) {
                   this.sexList= sexList;
         }

this

        其中username,password,sex,country, about, community,mailingList必須與form表單中的name屬性的字段同樣,不然將不能被Struts 2接收。

        下面來看看struts.xml配置文件:

 

<action name="getList" class="com.iman.action.RegisterAction"method="getList" >
            <result name="list">/register.jsp</result>
        </action>
        <action name="register" class="com.iman.action.RegisterAction"method="register">
            <result name="success">/results/registerSuccess.jsp</result>
        </action>


 

       有兩個action,其中一個action是將list傳到頁面,而另外一個action則是將註冊表單的信息傳到一個接受頁面,這個頁面就是registerSuccess.jsp,代碼以下:<%@ page language="java"contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s"uri="/struts-tags" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>註冊成功</title>
</head>
<body>
姓名:<s:property value="username"/><br>
性別:<s:property value="sex"/><br>
國家:<s:property value="country"/><br>
自我介紹:<s:property value="about"/><br>
所屬行業:<s:property value="community"/><br>
郵件列表:<s:property value="mailingList"/>
</body>
</html>
 


 

        好了,開始運行這個表單了,啓動服務器並部署項目,在地址欄輸入:http://localhost:8000/Struts2Tutorial/getList.action(想一想爲何要直接從action跳轉到頁面,我想我不說你也知道吧,當時我學習時但是費了好大勁兒才明白過來!從action跳轉完成了數據的初始化),頁面以下:



      輸入數值,點擊提交按鈕,將跳轉到registerSuccess.jsp頁面,效果以下:




      是否是很簡單啊?你只是看着簡單而已,經過這個表單,我對Struts2的標籤有了一個新的認識,你呢?


這是本人學習的結果,歡迎轉載,歡迎交流,但轉載務必給出本文章的連接地址:http://blog.csdn.net/youqishini/article/details/7060344,謝謝~

 

注意: <s:select name="country" label="國家" list="countryList" listKey="id" listValue="countryName"  />

listValue 是在頁面上顯示內容(countryName),listKey是要傳到後臺的內容(id)

相關文章
相關標籤/搜索