使用struts步驟:
- 在web.wml中載入struts
- 業務邏輯控制器,真正要工作的類別:HelloAction.java
- 寫一個會呼叫那個類別來工作的頁面:form.jsp
- 寫一個用來寫是結果的頁面:hello.jsp
- 把2-4的運作登記在struts.xml中
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts_Test11</display-name>
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<!-- 上面/*代表監視所有請求,如果有多個filter,須確保StrutsPrepareAndExecuteFilter放在最下面 -->
</filter-mapping>
</web-app>
HelloAction.java
package com.action;
public class HelloAction {
private String ename = "World";
public String getEname() {
System.out.println("3. get 方法被 view 的 EL 或 OGNL取值時自動呼叫....................................................");
return ename;
}
public void setEname(String ename) {
System.out.println("1. set 方法被 Struts 2 自動呼叫.....................................................");
this.ename = ename;
}
//execute是用來等action執行完之後告知要導入哪個頁面的
//需要把這隻程式註冊在struts.xml中
//因為是POJO檔,所以一定要有execute(),要執行的方法也要寫在裡面,因為沒有繼承其他Action類別
public String execute() {
System.out.println("2. execute 方法被 Struts 2 自動呼叫.....................................................");
return "success";
//struts.xml會依照return的結果(success、error等)來決定導入哪個頁面
}
}
from.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="Big5"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- ↑這是struts2的UI表單標籤 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>form.jsp</title>
<s:head theme="xhtml" />
<!--預設為 xhtml -->
<!--theme 可為 xhtml 或 simple 或 css_xhtml -->
</head>
<body>
<br>
<OL>
<LI><font color="blue"><b>傳統的Html標籤</b></font> <!--觸發Struts的URL預設為.action,也就是結尾要是.action才能觸發StrutsPrepareAndExecuteFilter(核心控制器) -->
<form
action="<%=request.getContextPath()%>/myNamespace/myAction.action"
method="post">
<!--/myNamespace是struts.xml中註冊的package名稱,/myAction是裡面註冊的action名稱(對應到特定類別),傳統標籤加上「.action」才能呼叫到struts
但其實現在可寫可不寫
getContextPath()取的環境路徑,也就是根目錄-->
員工姓名: <input type="text" name="ename" value="peter1吳永志" />
<p>
<input type="submit" />
</form> <BR></LI>
<LI><font color="blue"><b>Struts2的表單UI標籤</b></font> <!--↓前面是s:代表他是Struts2的表單UI標籤,預設 method="post"-->
<s:form action="myAction" namespace="/myNamespace">
<!-- 前面已經有註明是struts表單了,他會自己去struts.xml找到namespace跟底下的action(尋找是否有相符者),有就送過去,所以不行自己打.action -->
<s:textfield name="ename" label="員工姓名" value="peter1吳永志" />
<s:submit value="送出" />
正常:http://localhost:8080/Struts_Test11/myNamespace/myAction.action<br>
故意寫action="myAction.action":http://localhost:8080/Struts_Test11/myAction.action(不幫你補myNamespace)<br>
都不寫:沒反應
</s:form></LI>
</OL>
</body>
</html>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="Big5"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%-- request.setCharacterEncoding("UTF-8"); --%> <%-- 在Struts 2 中已可省略 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>hello.jsp</title>
<s:head theme="xhtml" /> <!--預設為 xhtml --> <!--theme 可為 xhtml 或 simple 或 css_xhtml -->
</head>
<body>
<h3>我是展示層 (view) hello.jsp</h3>
<UL>
<LI> Hello, ${requestScope.ename} 【EL取值】</LI>
<!-- 也可以是:{ename},會自己從小範圍找到大範圍 -->
<LI> Hello, <s:property value="ename" /> 【OGNL取值】</LI>
</UL>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- 上面的要自己上往找,下面自己打 -->
<struts>
<constant name="struts.devMode" value="true" />
<!-- package的設定用以告知struts2有哪些action可供呼叫,並可以對package內的action做統一管理 name="myPackage"==>package邏輯分類
name space="/myNamespace"==>起始URL,所有底下action的共同命名空間,存取action時必須加上這個 ,例如 myNamespace/myAction.action -->
<package name="myPackage" namespace="/myNamespace" extends="struts-default">
<!-- struts-default是預設定義在struts2中的package(jar檔),在此繼承他 -->
<!-- action:註冊,業務邏輯單元class class="com.action.HelloAction"==>被註冊的套件名稱+類別名稱(完整類別名稱)
name="myAction"==>註冊檔中被註冊的名字(識別字),讓外界可以呼叫 -->
<action name="myAction" class="com.action.HelloAction">
<!-- result可以有多個,引導到不同的頁面 success是預設,可略F result name="success"==>對應到HelloAction.java中的execute(),代表本方法執行順利後轉到/hello.jsp
頁面 -->
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>