JavaWeb學習總結——通用標籤和表單標籤(尚硅谷_佟剛老師)

通用標籤的示例

一、示例一:通過<s:property/>獲取屬性參數

1. 設置屬性值

<a href="Test.action?name=zzh">Test </a>

       <br><br>

<%

              session.setAttribute("date", new Date());

%>

2. 配置Struts2

<action name="TestServletActionContext"

                     class="zzh.struts2.com.TestServletActionContextAction">

              <result >/success.jsp</result>

       </action>

3. 輸出的jsp

首先導入<%@ taglib prefix="s" uri="/struts-tags" %>

<s:property value="#session.date"/>

       <br><br>

       <s:property value="#parameters.name[0]"/>

二、其他Struts2的通用標籤

1. 通過<s:url>獲取屬性參數

                          

2. 通過<s:set>向域對象添加屬性

3. 通過<s:put>壓入彈出

4. 通過<s:if>直接使用值棧中的屬性做條件判斷

5. iterator

6. sort

7. date

三、示例二、sort

index.jsp

    <a href="mytest">Mytest</a>

Struts.xml

<action name="mytest"

              class="zzh.struts2.com.MytestMethos" method="Mytest">

              <result>/mysuccess.jsp</result>

</action>

MytestMethod

publicclass MytestMethos {

 

       private List<Person> persons = new ArrayList<Person>();

       public List<Person> getPersons() {

              return persons;

       }關鍵代碼,切記

       public String Mytest() {

              persons.add(new Person("DD",55));

              persons.add(new Person("AA",22));

              persons.add(new Person("CC",44));

              persons.add(new Person("BB",33));

              return"success";

       }

}

mysuccess.jsp

       <%

              PersonComparator pc = new PersonComparator();

              request.setAttribute("comparator", pc);

       %>

       <s:sort comparator="#request.comparator" source="persons" var="persons2"></s:sort>

       <s:iterator value="#attr.persons2">

              ${Name }+${Age }<br>

       </s:iterator>

四、表單標籤初體驗——示例三

1.     實現

form.jsp(<%@ taglib prefix="s" uri="/struts-tags" %>)

       <s:form action="save">

              <s:hidden name="userId"></s:hidden>

              <s:textfield name="userName" label="UserName"></s:textfield>

              <s:password name="password" label="PassWord" showPassword="true"回顯></s:password>

              <s:textarea name="desc" label="Desc"></s:textarea>

              <s:submit></s:submit>

       </s:form>

Struts.xml

<action name="save" class="zzh.struts2.com.UserAction" method="save">

       <result name="input">/form-tag.jsp</result>

</action>

publicclass UserAction {

       private String userId;

       private String userName;

       private String password;

       private String desc;

<getter/setter>

       public String save() {

              System.out.println(this);

              return"input";

       }

2. htmlform相比

Ø  Struts2form標籤會生成一個table,自動排版

Ø  可以對錶單進行自動回顯

3.     測試回顯

回顯是吧棧頂對象開始匹配屬性,並把匹配的屬性值輔導對應的標籤的value中,若棧頂對象中沒有對應的屬性,則一次向下尋找

表單回顯測試代碼

       public String save() {

              System.out.println(this);

             

              UserAction uAction = new UserAction();

              uAction.setDesc("Oracle");

              uAction.setUserId("1001");

              uAction.setPassword("122121");

              uAction.setUserName("ATGUIGU");

             

              ActionContext.getContext().getValueStack().push(uAction);

             

              return"input";

       }

五、選擇

5.1checkbox

<s:checkbox name="married"

             label="Married"

             fieldValue="sss"></s:checkbox>

三個屬性。其中如果有fieldValue則提交表單中checkbox的值爲sss如果沒有fieldValue則爲true/false

5.2checkboxlist

Ø  ListlistKeylistValue

Ø  OGNL賦值

Ø  Map賦值

Ø  List賦值

<s:radio name="gender" list="#{'1':'Male','0':'Female'}" label="Gender"></s:radio>

<!—服務端需要使用集合類型,以保證能夠正常的回顯-->不用數組,用集合

<s:checkboxlist name="citys"

       list="#request.citys" listKey="cityId" listValue="cityName"

       label="City"></s:checkboxlist>

5.3 select

<s:select name="age" label="Age"list="{11,12,13,14,15,16,17,18,19,20}"

       headerKey="" headerValue="請選擇">

<s:optgroup label="21-30" list="#{21:21,22:22,222:333 }">

</s:optgroup>

    <s:optgroup label="31-40" list="#{31:31,32:32,222:333 }">

</s:optgroup>

</s:select>

<s:optgroup>可以用做s:select 的子標籤,必須是鍵值對

六、form-tag.jsp

<%

              List<City> cities = new ArrayList<City>();

              cities.add(new City(1,"北京"));

              cities.add(new City(2,"南京"));

              cities.add(new City(3,"太原"));

              cities.add(new City(4,"西安"));

             

              request.setAttribute("cities", cities);

       %>

      

       <s:debug></s:debug>

      

       <s:form action="save">

              <s:hidden name="userId"></s:hidden>

              <s:textfield name="userName" label="UserName"></s:textfield>

              <s:password name="password" label="PassWord" showPassword="true"></s:password>

              <s:textarea name="desc" label="Desc"></s:textarea>

             

              <s:checkbox name="married" label="Married"></s:checkbox>

             

              <s:radio name="gender" list="#{'1':'Male','0':'Female'}" label="Gender"></s:radio>

             

              <s:checkboxlist name="cities"

              list="#request.cities" listKey="cityId" listValue="cityName"

              label="City"></s:checkboxlist>

             

              <s:select name="age" label="Age" list="{11,12,13,14,15,16,17,18,19,20}"

              headerKey="" headerValue="請選擇">

                     <s:optgroup label="21-30" list="#{21:21,22:22,222:333 }"></s:optgroup>

                     <s:optgroup label="31-40" list="#{31:31,32:32,222:333 }"></s:optgroup>

             

              </s:select>

             

              <s:submit></s:submit>

       </s:form>

 七、主題

7.1修改主題:

Ø  整體:在<s:form action=”emp-save” theme=”simple”></s:form>

Ø  局部:不多見

Ø  在域對象中

    <%

       request.setAttribute("theme", "simple");

    %>

Ø  全局的:在struts.xml中修改

<conatant name=”struts.ui.theme” value=”simple”></constant>

7.2 主題類型

Ø  Simple

Ø  Xhtml

Ø  Css

Ø  Ajax


發佈了45 篇原創文章 · 獲贊 26 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章