1类缤、開發(fā)準備
2芜抒、第一個案例—Hello World
2.1.新建web project項目
2.2.配置項目
2.2.1 將開發(fā)庫導入lib文件夾下
2.2.2 在src包下新建struts.xml配置文件
2.2.3 修改struts.xml
2.2.4 修改web.xml文件為
2.2.5 訪問
2.3.原理
3槐沼、namespace
4、action
5痛阻、path
5.1 path.jsp
5.2 index.jsp
6毯炮、ActionMethod_DMI_動態(tài)方法調用
6.1 index.jsp頁面
6.2 struts.xml文件
6.3 UserAction.java
1份殿、開發(fā)準備
jdk(http://www.oracle.com/technetwork/java/javase/downloads/index.html)
myeclipse
tomcat(http://tomcat.apache.org/)
struts2(http://struts.apache.org/download.cgi)
Struts2開發(fā)文檔(http://struts.apache.org/docs/getting-started.html)
2、第一個案例—Hello World
2.1.新建web project
項目
File
-new
-web project
枝秤,輸入項目名醋拧,如sturts2_001
。
2.2.配置項目
2.2.1 將開發(fā)庫導入lib文件夾下
2.2.2 在src包下新建struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<!-- Add packages here -->
</struts>
2.2.3 修改struts.xml
在 下面添加:
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
說明:
①<constant name="struts.devMode" value="true" />開發(fā)模式
②action name:訪問的名稱
③package:類似于java中的包淀弹,解決重名問題
2.2.4 修改web.xml文件為
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<welcome-file-list>
<welcome-file>Hello.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-mapping>
</web-app>
2.2.5 訪問
新建Hello.jsp文件丹壕,然后部署項目,訪問localhost:8080/struts2/hello即可訪問到Hello.jsp頁面
2.3.原理
3垦页、namespace
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
namespace決定了action的訪問路徑雀费,默認為"",可以接收所有路徑的action
namespace可以寫為’/’痊焊,或者’/xxx’盏袄,或者’/xxx/yyy’忿峻,對應的action訪問路徑為’/index.action’‘/xxx/index.action’,或者’/xxx/yyy/index.action’辕羽。
namespace最好也用模塊來進行命名
4逛尚、action
action不一定是servlet,可以是一個普通類刁愿。
<action name="path" class="com.bjsxt.struts2.path.action.PathAction">
“class”是類的路徑绰寞。當class屬性沒有配置時,默認訪問ActionSupport類铣口。
action的三種實現(xiàn)方法
①普通類滤钱,手寫execute方法。缺點:容易出錯脑题。
public class IndexAction1 {
public String execute() {
return "success";
}
}
②實現(xiàn)Action類件缸,重載execute方法。
public class IndexAction2 implements Action {
@Override
public String execute() {
return "success";
}
}
③繼承已經實現(xiàn)好的類叔遂,有很多可以直接用方法他炊。推薦使用
public class IndexAction3 extends ActionSupport {
@Override
public String execute() {
return "success";
}
}
5、path
struts2中的路徑問題是根據(jù)action的路徑而不是jsp路徑來確定已艰,所以盡量不要使用相對路徑痊末。
雖然可以用redirect方式解決,但redirect方式并非必要哩掺。
解決辦法非常簡單凿叠,統(tǒng)一使用絕對路徑。(在jsp中用request.getContextRoot方式來拿到webapp的路徑)
或者使用myeclipse經常用的嚼吞,指定basePath幔嫂。
5.1 path.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
struts2中的路徑問題是根據(jù)action的路徑而不是jsp路徑來確定,所以盡量不要使用相對路徑誊薄。<br />
<a href="index.jsp">index.jsp</a>
<br />
雖然可以用redirect方式解決履恩,但redirect方式并非必要。
<br />
解決辦法非常簡單呢蔫,統(tǒng)一使用絕對路徑切心。(在jsp中用request.getContextRoot方式來拿到webapp的路徑)
<br />
或者使用myeclipse經常用的,指定basePath
</body>
</html>
注:前面設置好了<base>標簽片吊,就可以使用絕對路徑了绽昏。
5.2 index.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%--
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//在head中<base href>指定basePath
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href="path/path">路徑問題說明</a>
</body>
</html>
6、ActionMethod_DMI_動態(tài)方法調用.
Action執(zhí)行的時候并不一定要執(zhí)行execute方法俏脊,也可以訪問其他方法全谤。
訪問方法有兩種:
①在配置文件中配置Action的時候用method=“”來指定執(zhí)行指定方法
②url地址中動態(tài)指定(動態(tài)方法調用DMI)(推薦)
6.1 index.jsp頁面
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
Action執(zhí)行的時候并不一定要執(zhí)行execute方法<br />
可以在配置文件中配置Action的時候用method=來指定執(zhí)行哪個方法
也可以在url地址中動態(tài)指定(動態(tài)方法調用DMI)(推薦)<br />
<a href="<%=context %>/user/userAdd">添加用戶</a>
<br />
<a href="<%=context %>/user/user!add">添加用戶</a>
<br />
前者會產生太多的action,所以不推薦使用
</body>
</html>
說明:第一個超鏈接是普通方法爷贫,對應struts.xml中第一個action认然,需要配置method屬性补憾。
第二個超鏈接是動態(tài)方法調用,struts.xml文件不需要改動卷员,推薦使用盈匾。
6.2 struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">
<action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action>
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
6.3 UserAction.java
package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
}