Struts2的開發流程
為了能夠在eclipse中使用Struts2在進行開發時,需要根據需要導入一些有關的jar包;
在官網下載相關的壓縮包,這裡下載了兩個:struts-2.3.30-all.zip和struts-2.3.30-docs.zip,解壓到同一個文件夾中。
在eclipse中新建了新的Dymamic Web Project以後,為該項目添加Struts2支持:
1.復制Struts2解壓目錄下struts2-blank.war解壓後WEB-INF\lib目錄下的所有jar包到web項目中的WebContent\WEB-INF\lib下;
2.把這些復制的jar包導入項目中去。
開始使用Struts2開發:
1.在web.xml中定義攔截器攔截用戶請求:
如果eclipse中生成的Dynamic Web Project中沒有自動生成web.xml文件,可以從Tomcat相關的文件夾中找到一個默認的web.xml文件,把此文件復制到項目的WEB-INF目錄下(不是WEB-INF\lib下)。
也可以在struts2-blank.war解壓後目錄的WEB-INF下直接復制一個web.xml。
修改此web.xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 定義核心攔截器 -->
<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-mapping>
</web-app>
2.定義包含表單數據的JSP頁面:
寫一個提交請求的JSP頁面,此JSP頁面將會將請求提交給相關的Action類來處理。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>Insert title here</title>
</head>
<body>
<s:form action="test_1">
<s:textfield name="username" key="username"/>
<s:submit key="login" value="login"/>
</s:form>
</body>
</html>
3.定義處理用戶請求的Action類:
寫一個與以上JSP關聯的Action類來處理該JSP提交的請求。在eclipse中直接放在在src目錄下即可
package testAction;
import com.opensymphony.xwork2.ActionSupport;
public class Test_1Action extends ActionSupport{
//定義封裝請求參數的成員變量
private String username;
//setter、getter方法
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
//定義處理請求的execute方法
public String execute()throws Exception{
if(getUsername()!=null){
return SUCCESS;
}else{
return ERROR;
}
}
}
4.編寫物理視圖資源文件:
為了簡單,我們把物理視圖資源文件直接放在項目的webContent目錄下
error.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>error page</title>
</head>
<body>
錯誤頁面!username為空!
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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>welcome page</title>
</head>
<body>
登陸成功!
</body>
</html>
5.配置處理用戶請求的Action類並配置Action的處理結果和物理視圖資源的映射關系::
使得JSP頁面的請求能夠准確找到與此JSP頁面相關的Action類。
配置Action需要在項目的src目錄下添加struts.xml文件,此文件的默認文件也可以在struts2-blank.war解壓後目錄的WEB-INF\classes下找到,把其修改為如下內容:
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="test" extends="struts-default">
<!--
定義了名為test_1的Action;
處理請求的Action類為testAction.Test_1Action.java
-->
<action name="test_1" class="testAction.Test_1Action">
<!-- 定義邏輯視圖與物理視圖之間的映射關系 -->
<result name="error">error.jsp</result>
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>