1.首先下載Struts2 必要的包(大家可以到我的云盤直接下載 struts2 必要包 如果失效了,大家也可以自行下載,我會(huì)在下面放一張所需包的圖片)
2.然后在 eclipse 中創(chuàng)建一個(gè) 動(dòng)態(tài)web工程
3.然后大家把之前的下載的包全部拷貝到 WEBContent/WEB-INF/lib/ 這個(gè)目錄下面。(注意:如果你是在我云盤中下載的,其中有兩個(gè)文件web.xml 和 struts.xml 不用拷貝到 lib目錄下面,這兩個(gè)文件后面會(huì)用到)
4.然后在 WEB-INF 目錄下面生成一個(gè) web.xml 文件(當(dāng)然你也可以直接將我的文件拷貝到該目錄下),如果你是自己生成的,那么你就將下面的代碼拷貝到這個(gè)文件中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<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>
當(dāng)啟動(dòng)一個(gè)web項(xiàng)目時(shí),web容器(這里指tomcat)會(huì)去讀取他的配置文件 web.xml,上面的代碼表示可以攔截到所有的 url 請(qǐng)求。
5.在 java Resources/src/ 目錄下生成一個(gè) 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>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="han.LoginAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
上面代碼的意思就是 攔截到有一個(gè)
login.action的請(qǐng)求,因?yàn)槲覀冊(cè)诘卿涰?yè)面里面的 action 中寫的 login,所以攔截器會(huì)攔截到這個(gè)請(qǐng)求,然后執(zhí)行han包下面的LoginAction類中的execute方法,根據(jù)這個(gè)方法返回的字符串來(lái)決定跳轉(zhuǎn)到哪一個(gè)頁(yè)面(現(xiàn)在我們還沒有創(chuàng)建han包,它會(huì)在后面創(chuàng)建)
6.這時(shí)基本把struts2 的環(huán)境給配置好了。
7.在 WebContent/ 目錄下生成三個(gè) jsp 文件(注意:不要把jsp文件放到 META-INF 和 WEB-INF 下面了),以下是三個(gè)jsp文件:
loginForm.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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user" />
<s:textfield name="password" key="pass" />
<s:submit key="login" value="submit" />
</s:form>
</body>
</html>
不用管
<s:textfield.../>這個(gè)標(biāo)簽是什么意思,為什么與 html 標(biāo)簽不同,因?yàn)檫@時(shí) struts2 特有的標(biāo)簽
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
wow something wrong
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is successful page
</body>
</html>
8.在 src 目錄下生成一個(gè) han包(這個(gè)包名隨便,只是我這個(gè)例子中使用的是這個(gè)包而已,大家可以自行取名,但是相應(yīng)在 struts 中 action 的class 也要改變),在這個(gè)包下面新建一個(gè) LoginAction 類,這個(gè)類繼承自 ActionSupport,并且重寫 execute方法,代碼如下;
package han;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
//定義封裝請(qǐng)求參數(shù)的用戶名和密碼
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LoginAction() {
}
//定義處理用戶請(qǐng)求的 execute方法,這個(gè)方法會(huì)被程序自動(dòng)調(diào)用
public String execute() throws Exception {
//當(dāng)用戶名與密碼都為 123 的時(shí)候,返回成功字符串,否則返回 錯(cuò)誤字符串
if(getUsername().equals("123") && getPassword().equals("123")){
System.out.println(5656);
ActionContext.getContext().getSession().put("user", getUsername());
System.out.println(3344);
return SUCCESS;
}
return ERROR;
}
}
9.這時(shí)基本上已經(jīng)完成了,然后右鍵 loginForm.jsp,運(yùn)行run as命令,啟動(dòng)服務(wù)器,進(jìn)入 登錄頁(yè)面,如果用戶名和密碼都輸入 123,那么進(jìn)入 success.jsp 頁(yè)面,否則進(jìn)入error.jsp頁(yè)面。