使用IntelliJ IDEA 生成Struts2框架應用實例Hello World
當在Struts2的web應用程序中點擊一個超鏈接或提交一個HTML表單時,會由控制器收集輸入併發送一個叫Actions的Java類。
Action被執行後,Result會選擇一個資源給出響應。
對於每個Struts2項目來的構建來說要有以下四個組件:
- Action 操作
創建一個動作類,包含完整的業務邏輯並控制用戶、模型及視圖間的交互。
- Interceptors 攔截器
這是控制器的一部分,可以根據需求創建攔截器,或者使用現有的攔截器
- View 視圖
創建一個JSP與用戶進行交互,獲取輸入並呈現最終信息
- Configuration Files 配置文件
創建配置文件來連接動作、視圖以及控制器,這些文件分別是struts.xml,web.xml及Struts.properties
使用IntelliJ IDEA 生成Struts2框架的J2EE應用程序步驟如下:
第一步:
File->new->Project...
在彈出的對話框中選擇Java Enterprise
第二步:
對生成的應用程序加入Struts2框架集成
Additional Libraries and Frameworks中選擇Struts2,選擇“Next”
第三步:
在完成第二步後在新的窗口中輸入Project name
創建Action類
Action類它是Struts2應用程序的關鍵,通過它實現大部分業務邏輯。
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private String name;
@Override
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Struts2框架會創建一個HelloWorldAction類對象,並調用execute方法來響應用戶的動作。
創建視圖
創建一個JSP來展現最終信息,當一個預先定義動作發生時空上頁面將被Struts2框架調用,並且這個映像會定義到struts.xml文件裏。
<
</html>
%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World,<s:property value="name"/>
</body>
taglib指令告知Servlet容器這個頁面會使用Struts2標籤,並且這些標籤會被s放在前面,s:property標籤顯示Action類“name”屬性的值,這個值是通過HelloWorldAction類的getName()方法返回的。
對index.jsp頁面改造
<
有一個form表單,其中action定義爲hello,action會通過struts.xml文件映射到HelloWorldAction類及其execute方法。
%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World From Sturuts2</title>
</head>
<body>
<form action="hello">
<%--@declare id="name"--%>
<label for="name">請輸入你的名字</label>
<input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
配置文件
需要一個映像把URL,HelloWorldAction類(模型)以及HelloWorld.jsp(視圖)聯繫在一起。映像告知Struts2哪個類將響應用戶的動作(URL),類的哪個方法會執行,以及基於方法所返回的字符串結果,會呈現怎麼樣的視圖。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloworld" extends="struts-default">
<action name="hello" class="com.xiaoxie.struts2.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
helloworld數據包(package),在這個數據包中把Actions集合在一起。其中命名動作action名稱爲hello,並指定對應的Action處理類同時指定其執行方法名稱爲execute。在action中指定返回結果爲success時會進入哪個頁面進行顯示結果。
web.xml文件,這個文件是Struts2任何請求的接入點。在web.xml中Struts2應用程序的接入口會定義爲一個過濾器。
<
?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在上面配置了所有的URL(/*)都會走到Struts2過濾器
注意:生成的<filter-class>中的類如果找不到一定要根據當前的Structs包中的類來對應加入,工具默認生成的可能不正確(有版本問題)
在WEB-INF下新增lib目錄,並把struts2相關的jar包複製進去,複製進後一定要對選中的類右鍵選擇Add Library