Struts2 入門學習筆記詳解:
Struts2起源與背景:
學習一個技術就要了解它的歷史, 知道它的背景 出現的因素… 這是我學習的習慣
在以前很久一段時間曾經出現過一種框架Struts1
Struts1
在很長一段時間, 在MVC框架中處于絕對的統治地位…大概04年吧… 我才上幼稚園…
Struts1:可以很好的實作 控制與業務邏輯的分離…
但自身也存在一些缺陷:
表現層單一: 只支持表現層使用, 限制了Struts1 的發展;
對Servlet APl 的依賴: Struts1 框架基于 Model2 模式開發, 因此其中需要涉及大量的Servlet APl 進一步對Web容器產生了依賴;
不利于代碼重用::在代碼開發中除了自己定義了類外 還必須使用Struts1 中的類; 型別之間的耦合嚴重!
WebWork
為了解決此類問題, 又出現了一種新的技術WebWork;
它更加強調 系統之間的松耦合:使用攔截器來實作控制, 由此不在依賴于web容器
松耦,表現層支持更多的視圖,使開發更加靈活; 但由于宣傳不高 使用率并不多…名氣并不大!! 為了提高訪問度 改了個名字 Struts2 出現了;
Struts2
以WebWork框架的設計思想為核心,吸收了Struts 1的部分優點;
WebWork + Struts1 = Sturts2
Struts 2的獲取
Struts 2官方地址:http://struts.apache.org
本次選取Struts 2.3.16.3進行講解

Struts2 應用:
引入Struts2 Jar
新建Java Web專案, 引入Jar檔案添加至專案中lib
Struts2專案基礎jar檔案:
| 檔案明 | 說明 |
|---|---|
| struts2-core-xxx.jar | Struts 2框架的核心類別庫 |
| xwork-core-xxx.jar | XWork類別庫,Struts 2的構建基礎 |
| ognl-xxx.jar | Struts 2使用的一種運算式語言類別庫 |
| freemarker-xxx.jar | Struts 2的標簽模板使用類別庫 |
| javassist-xxx.GA.jar | 對位元組碼進行處理 |
| commons-fileupload-xxx.jar | 檔案上傳時需要使用 |
| commons-io-xxx.jar | Java IO擴展 |
| commons-lang-xxx.jar | 包含了一些資料型別的工具類 |
| …jar | 這里僅是一些基礎的Jar 隨著程式擴展還有很多個jar 要不停學習哦~ |
開始第一個Struts2 專案:

配置web.xml
撰寫web專案, Struts2框架所需要的過濾器:web.xml (個人喜歡先弄配置, 一般都是cope)
注意:
目前由于開發工具的進步, 可能有些web專案默認不會在顯示 web.xml檔案但一定不是手創的!可以設定工具專案版本——myelicpse
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 專案名 -->
<display-name>Struts2</display-name>
<!-- web檔案指定的默認訪問頁面.. -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Struts2過濾器 -->
<!-- 作用:啟動加載struts.xml檔案-->
<filter>
<filter-name>struts</filter-name>
<!-- Struts2核心控制器類:
早期還是:org.apache.struts2.dispatcher.FilterDispatcher
-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 現在! -->
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern> <!-- /*: 任何請求都要經過.. -->
</filter-mapping>
</web-app>
撰寫頁面 :
這里只是先單純的學習Struts 并不會與資料庫打交道訪問資料庫啥的…
Struts 就像之前學習SpringMVC一樣, 相當于一個控制器 Servlet一樣 接收頁面請求回應結果資料
(一般搭配Hibernate 進行資料庫互動這里暫時不講解 歡迎可以觀賞以下我之前的博客有詳細講解… );
這里頁面使用: JSP
HelloWord.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!-- 引入Struts2標簽庫 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>${msg }</p> <!-- 單純直接使用EL是獲取不到滴...EL是獲取作用域中的資料都沒有存哦~ -->
<h1>
<s:property value="msg"/> <!-- 使用Struts2標簽庫,可以直接獲取到回應action的全域變數的資料; -->
</h1>
<!-- 表單: name="name" 可以用于傳遞引數.. -->
<form action="hello" method="post">
名字: <input type="text" name="name"> <input type="submit"value="提交">
</form>
</body>
</html>
撰寫Action Java類;(相當于控制器的操作)
個人喜歡在撰寫時, 專門建一個包 action包 用于專門存在 action類;
這里先介紹一下最基礎的實作Action介面方式來實作,重寫 execute();
根據回傳值String 找到對應的回應頁面…
仔細一看有點像Servlet了,對于多種不同請求如果采用該方式不是很方便, 多重判斷執行 回傳不同結果頁面…
后面會深入擴展多種的Action實作方式…
HelloWordAction.java
package com.wsm.action;
//注意別倒錯包了
import com.opensymphony.xwork2.Action;
/*Action:
* Struts中用于接收回應,請求的Java類,相當于 "控制器"
*
* 全域變數:
* Struts中用于 "接收"或"顯示" 資料;
* 需要給其提供給get/set();
* 接收頁面資訊: 變數名要和表單name名相同;
* 向頁面輸出: 頁面可以通過: Struts2運算式/或存在作用域中通過EL運算式取出進性資料展示;
*/
public class HelloWordAction implements Action {
//獲取頁面表單名字;
private String name;
//等會向頁面輸出的變數...
private String msg;
//Struts2 還支持使用復雜的資料型別: 集合 陣列 物件...
//實作方法
@Override
public String execute() throws Exception {
System.out.println(name);
msg="hello:"+name; //拼接向頁面輸出的資訊;
return "hello"; //回傳 "邏輯視圖名",struts.xml根據:<result name="回傳的邏輯視圖名" >找到最后的回應頁面</result>
}
/*
對于return "邏輯視圖名"; Action介面中還提供了5個字串型別靜態常量:
用于更加規范的,指定對應的 "邏輯視圖";
其實也就是普通的字串,并無特殊機制.. 邏輯視圖指定的頁面也還是要自己定義的,只是Struts2的一種完善機制吧;
*/
//get/set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Action介面常量字串邏輯含義
| 常量 | 值 | 邏輯含義 |
|---|---|---|
| SUCCESS | “success” | 表示程式處理正常, 并回傳給用戶成功的結果; |
| NONE | “none” | 表示程式處理正常,但不回傳給用戶任何顯示… |
| ERROR | “error” | 回傳處理結果失敗… |
| INPUT | “input” | 表示需要更多用戶輸入才能順利執行 |
| LOGIN | “login” | 表示用戶需要正確登錄后才能順利執行 |
創建Struts2 組態檔:
頁面發送請求——經過web,xml過濾——Struts2組態檔: 處理——
找到對應的xxaction類 一系列操作后回傳一個"邏輯視圖名"——至Struts2組態檔根據result 找到對應回應頁面…;
注意: 該檔案必須建在 專案的Src檔案下,部署服務器時候才會在: WEB-INF/classes 目錄下的,才可以正確獲取執行…
一般取名:struts.xml
struts.xml
在 src 目錄下新建一個名稱為 struts.xml 的檔案
<?xml version="1.0" encoding="UTF-8"?>
<!-- struts.xml宣告部分,工具沒有配置的DTD檔案,可以直接cope下面的... -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- struts.xml根節點 -->
<struts>
<!-- 創建: 一個default包名, namespace命名空間 "/"表示尋找根目錄下請求action.., 繼承struts-default(Struts2底層的...) 后面詳細講解...-->
<package name="default" namespace="/" extends="struts-default" >
<!-- action:用于接收頁面的請求,指定對應的action類中執行...(package中可以配置多個)
name:根據頁面請求根據name找到指定的action class:指定對于action java類進行執行;
-->
<action name="hello" class="com.wsm.action.HelloWordAction" >
<result name="hello" >/HelloWord.jsp</result> <!-- 根據 xxxaction類,回傳的邏輯視圖名指定對于的回應頁面... -->
<!-- 如果沒有指定 name默認值就是success,所以action returu "success"; 可以不指定name -->
</action>
</package>
</struts>
Struts2使用物件接收用戶輸入資料 / 訪問Servlet Apl
使用Struts2實作登錄功能:
在上一專案基礎上擴展…

擴展一個物體類(物件資料傳輸);
User .Java
package com.wsm.entity;
public class User {
//屬性
private String userName;
private String userPwd;
//無參構造
public User(){};
//帶參構造
public User(String userName, String userPwd) {
super();
this.userName = userName;
this.userPwd = userPwd;
}
//get/set
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
登錄所需的login 頁面;
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login" method="post">
<!-- 對于物件型別的,讀取通過action類中: 物件名.屬性 進行賦值讀取-->
名字<input type="text" name="user.userName"> <!-- 對于屬性名要和Java類一致 且區分大小寫-->
密碼<input type="text" name="user.userPwd" >
<input type="submit" value="提交">
</form>
<!-- 因為控制器request中存盤了登錄失敗資訊,EL運算式查看~ -->
<p>${shibai }</p>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>登錄成功!${user.userName }</p>
</body>
</html>
登錄的action 及struts2訪問ServletApl物件;
struts2 訪問ServletApl物件
在Struts2 概述中介紹過, Struts2的Action 沒有與ServletApl 發送耦合;從而可以輕松實作Action 的測驗;
但是在web 開發程序中, 無法避免需要使用 ServletApl中的物件;
Struts2提供了多種方式實作ServletApl的訪問:(兩大類)
解耦方式操作ServletAPl 物件 兩種實作 (下面實體就是一種解耦形式) ;
- 使用ActionContext 類獲取ServletApl物件 (Map物件)
Struts2 框架使用普通的Map物件替代了:HttpServletRequest HttpSession ServletContext …
Action類中 ,可以輕易的獲取對應的Map型別的替代物件, 實作了解耦的形式…
解耦方式獲得作用域物件,如下代碼案例- Action類實作特定介面來實作 ServletApl 物件; (即采用注入方式實作)
好處: 這種方式有利于單元測驗, 因為ActionContext需要借助框架進行初始化, 而這種方式無須初始化直接獲取使用…
如下實體
Action類實作特定介面來實作 ServletApl 物件ServletAction.java
package com.wsm.action;
//注意匯入對應的包~
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.Action;
public class ServletAction implements Action, RequestAware,SessionAware,ApplicationAware {
//注意這里定義的也是一個 Action;
//只不過本次只是為了讓你們知道,可以通過實作對應介面,解耦獲得對應的 Servlet物件,所以沒有什么特定的實作功能...別搞混~
//自定義解耦的作用域物件;
Map request;
Map session;
Map application;
//action:實作的方法..這里沒有使用不詳細介紹了...
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return null;
}
//實作對應的方法,每一個介面都只有一個實作方法(); 用戶回傳對應的Map型別,模擬作用域物件;
@Override
public void setApplication(Map<String, Object> appliction) {
this.application = appliction;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
耦合方式操作ServletAPl 物件;
直接訪問 Servlet API將使Action類與Servlet API耦合在一起;
Servlet API物件均由Servlet容器來構造,與這些物件系結在一起,測驗程序中就必須有Servlet容器,這樣不便于Action類的測驗
但有時候,確實需要訪問這些物件,Struts2同樣提供了直接訪問ServletAPI物件的方式,
要直接獲取Servlet API物件可以使用org.apache.struts2.ServletActionContext類,該類是ActionContext類的子類,
//導包
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
HttpServletRequest request=ServletActionContext.getRequest(); //獲取requset
ServletContext application=ServletActionContext.getServletContext(); //獲取appliction
HttpSession session=request.getSession(); //根據requset 獲取session;
除了直接使用 ServletActionContext 來直接獲取Servlet Apl物件; 還可以通過實作介面注入ServletApl物件...省略了;
LoginAction.java
package com.wsm.action;
//注意別倒錯包了
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.wsm.entity.User;
public class LoginAction implements Action {
//全域變數
public User user; //物件型別,頁面通過 物件名.屬性名 可進行讀取操作...
//解耦方式獲得作用域物件
ActionContext ac = ActionContext.getContext();
Map<String,Object> request = (Map<String,Object>)ac.get("request"); //獲取request
Map<String,Object> session = ac.getSession(); //獲取session
Map<String,Object> appliction = ac.getApplication(); //獲取appliction
@Override
public String execute() throws Exception {
//驗證登錄成功/失敗! admin 123
if("admin".equals(user.getUserName()) && "123".equals(user.getUserPwd()) ){
//登錄成功!用戶存session
session.put("user",user);
return "index";
}
//往request 存盤失敗資訊;
request.put("shibai","登錄失敗");
return "login"; //失敗回到登錄頁面~
}
//get/set
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
action都寫好,最后一定不要忘記 struts2的組態檔!
就不寫全了…直接在 package 中加一組 action 即可~
struts.xml
<action name="login" class="com.wsm.action.LoginAction" >
<result name="index" >/index.jsp</result>
<result name="login" >/login.jsp</result>
</action>
Struts2 資料校驗:
對于一個web 應用而言, 所有的用戶資料都是通過瀏覽器, 收集的;
用戶的輸入資訊非常復雜多變,如果程序中 : 硬體設備/網路/用戶惡意破壞…輸入的資訊可能導致程式例外…
對于這種問題應用程式需要能夠在控制層, 處理例外的輸入…
通常的做法是, 控制層發現例外, 直接回傳回瀏覽器, 提示例外用戶必須重新輸入合法的…
對于這種操作,可以通過Struts2資料效驗機制,進行完善…
使用Struts2的資料校驗,action類將不再通過實作Action介面來實作..而是通過繼承 ActionSupport類來完善資料校驗操作
ActionSupport類
是一個默認的 Action介面的實作類,
這個類提供了很多默認的方法 :國際化資訊 資料校驗 默認處理用戶請求…等方法;
因為ActionSupport類 是Struts2的默認實作類. 所以Struts.xml 中 < action > 可以不指定 class屬性
ActionSupport類中的 validate() 實作資料校驗, 所以需要重寫…
給用戶登錄操作加非空驗證:
需要修改LoginAction
LoginAction.java
package com.wsm.action;
//注意別倒錯包了
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wsm.entity.User;
public class LoginAction extends ActionSupport { //繼承ActionSupport
//全域變數
public User user; //物件型別
//解耦方式獲得作用域物件
ActionContext ac = ActionContext.getContext();
Map<String,Object> request = (Map<String,Object>)ac.get("request"); //獲取request
Map<String,Object> session = ac.getSession(); //獲取session
Map<String,Object> appliction = ac.getApplication(); //獲取appliction
@Override
public String execute() throws Exception {
//驗證登錄成功/失敗! admin 123
if("admin".equals(user.getUserName()) && "123".equals(user.getUserPwd()) ){
//登錄成功!用戶存session
session.put("user",user);
return "index";
}
//往request 存盤失敗資訊;
request.put("shibai","登錄失敗");
return "login"; //失敗回到登錄頁面~
}
//重寫 validate(); 資料效驗的方法;
@Override
public void validate() {
if(user.getUserName().length()==0){
super.addFieldError("username", "用戶名不能為空");
}
if(user.getUserPwd().length()==0){
super.addFieldError("userpwd", "密碼不能為空");
}
}
/*
當頁面,發送一個請求,找對對應的action 時,最先進入就是 validate();方法中進行資料校驗..
如果在驗證程序中 addFieldError(); 添加了驗證資訊,那么就不會在進入execute(); 方法;
而是直接回傳一個 "input"邏輯視圖名,去struts.xml 找到指定的一個頁面...
所以一定要在 struts.xml對應action標簽中配置一個: <result name="input" >回應頁面</result>
頁面上可以通過:Struts2標簽來講校驗例外資訊展示..
*/
//get/set
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
struts.xml
<action name="login" class="com.wsm.action.LoginAction" >
<result name="index" >/index.jsp</result>
<result name="login" >/login.jsp</result>
<result name="input" >/login.jsp</result> <!-- input:表示資料例外回傳回登錄頁面; -->
</action>
修改頁面展示校驗結果:login.jsp
注意:別忘了匯入 <%@ taglib prefix=“s” uri="/struts-tags" %> < !-- Struts2標簽庫 -->
可以通過 <s:fielderror></s:fielderror> 進行資料例外資訊展示…
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!-- Struts2標簽庫 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 資料效驗例外資訊輸出展示.. -->
<div>
<s:fielderror></s:fielderror>
</div>
<form action="login" method="post">
<!-- 對于物件型別的,讀取通過action類中: 物件名.屬性 進行賦值讀取-->
<!-- 對于屬性名要和Java類一致 且區分大小寫-->
名字<input type="text" name="user.userName"> <s:fielderror fieldName="username" ></s:fielderror> <!-- 根據fieldName指定顯示例外的資訊.. -->
<hr/>
密碼<input type="text" name="user.userPwd" > <s:fielderror fieldName="userpwd" ></s:fielderror>
<input type="submit" value="提交">
</form>
<!-- 因為控制器request中存盤了登錄失敗資訊,EL運算式查看~ -->
<p>${shibai }</p>
</body>
</html>
Struts2標簽:
Struts2 中提供了很多功能強大的標簽庫, 并且遠遠超出了傳統的標簽庫的基本功能:(上面校驗資訊就是一種…)
主要分為兩大類:UI標簽 通用標簽
使用前需要匯入 Struts2標簽庫: <%@ taglib prefix=“s” uri="/struts-tags" %>
UI標簽
UI標簽可以分為三大類:表單標簽 非表單標簽 Ajax標簽
Struts2常用表單標簽:
| 標簽 | 說明 |
|---|---|
| <s:form>…</s:form> | 表單標簽 |
| <s:textfield>…</s: textfield> | 文本輸入框 |
| <s:password>…</s: password> | 密碼輸入框 |
| <s:textarea>…</s: textarea> | 文本域輸入框 |
| <s:radio>…</s: radio> | 單選按鈕 |
| <s:checkbox>…</s: checkbox> | 多選框 |
| <s:submit/> | 提交標簽 |
| <s:reset/> | 重置標簽 |
| <s:hidden/> | 隱藏域標簽 |
| <s:actionerror/> | 顯示Action錯誤 |
| <s:actionmessage/> | 顯示Action訊息 |
| <s:fielderror/> | 顯示欄位錯誤,輸出addFieldError("",""); 存盤的資訊… 一般用于 資料校驗! |
這些標簽會被瀏覽器, 翻譯加載運行, 可能會以一種 <table/><tr/><td/> 形式包裝輸出…
這是Struts2的默認格式:可以在struts.xml 對其默認風格進行修改:
<struts>
<!-- 設定用戶界面默認XHTML風格:value="值可選.." -->
<constant name="struts.ui.thems" value="simple" />
</struts>
通用標簽
Struts2 UI標簽主要用于視圖的設計,在視圖的設計程序中還往往伴隨著一些業務控制行為;
eg:判斷Action中讀取的集合是否為空,對集合進行 遍歷!
Struts2常用的通用標簽:

if/else if/else標簽
三個都是條件判斷 分支控制標簽,幾乎于Java類似:
三個標簽中只有
<s:if />可以單獨使用
<s:elseif /><s:else />可以和<s:if />搭配使用,但不能單獨使用!
iterator迭代標簽

實體Demo
登錄成功, 查詢所有用戶資訊遍歷,并且隔行變色
修改LoginAction.java
//修改execute();
@Override
public String execute() throws Exception {
//驗證登錄成功/失敗! admin 123
if("admin".equals(user.getUserName()) && "123".equals(user.getUserPwd()) ){
//登錄成功!用戶存session
session.put("user",user);
//存盤幾個用戶,模擬查詢,頁面測驗 struts2標簽庫,別忘了給物體類加一個帶參建構式(name,pwd);
List<User> users = new ArrayList<User>();
users.add(new User("張三1", "123"));
users.add(new User("張三2", "123"));
users.add(new User("張三3", "123"));
users.add(new User("張三4", "123"));
request.put("users",users);
return "index";
}
//往request 存盤失敗資訊;
request.put("shibai","登錄失敗");
return "login"; //失敗回到登錄頁面~
}
index.jsp 頁面輸出:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>登錄成功!${user.userName }</p>
<!-- s:iterator:迭代遍歷,獲取request中資料!都已經requset了其實也可以直接使用JSTL了..感覺雞肋了... -->
<s:iterator var="user" value="#request.users" status="status">
<!-- 判斷元素下標隔行變色 -->
<s:if test="#status.index % 2==0">
<div style="background-color: red">
<s:property value="#user.userName"/>---<s:property value="#user.userPwd"/>
</div>
</s:if>
<s:else>
<div style="background-color: blue;">
<s:property value="#user.userName"/>---<s:property value="#user.userPwd"/>
</div>
</s:else>
</s:iterator>
</body>
</html>
終于寫完了,感謝觀看,最后實體的隔行顏色有點丑,別建議~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/208230.html
標籤:其他
上一篇:超鏈接的偽類及如何清除快取
下一篇:js物件篇



