一、开发流程概述
1.在web.xml中定义核心Filter拦截用户请求,添加如下内容:
1 2 3 4 5 6 7 8 |
<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> |
2.根据需求编辑jsp请求代码
3.定义处理用户请求的Action类
4.配置Action,类似如下内容(struts.xml):
1 2 3 4 |
<action name="login" class="cn.no7player.struts2.action.LoginAction"> <!-- 定义三个逻辑视图和物理资源之间的映射 --> ... </action> |
5.配置处理结果和视图资源的关系:
1 2 3 4 5 6 |
<action name="login" class="cn.no7player.struts2.action.LoginAction"> <!-- 定义三个逻辑视图和物理资源之间的映射 --> <result name="input">/login.jsp</result> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> |
6.编写视图资源,一般借助OGNL表达式
二、操作实践——创建一个简单的struts2应用
1. 新建 Web Project :
(1)选择创建Web Project项目,命名为:StrutsSimple
(2)创建完成后初始化目录结构如图:
(3)* index.jsp初始化内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body> </html> |
(4)*web.xml初始化内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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>HelloStruts2</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> </web-app> |
2.下载配置struts2依赖包:
下载选项:
-
Full Distribution:
- struts-2.3.20-all.zip (65MB) [PGP] [MD5]
-
Example Applications:
- struts-2.3.20-apps.zip (35MB) [PGP] [MD5]
-
Essential Dependencies Only:
- struts-2.3.20-lib.zip (19MB) [PGP] [MD5]
-
Documentation:
- struts-2.3.20-docs.zip (13MB) [PGP] [MD5]
-
Source:
- struts-2.3.20-src.zip (7MB) [PGP] [MD5]
apps: 示例应用
docs: 技术文档
lib: 核心类库
src: 源代码
(3) * apps–>.war–>struts2-blank.war 解压(一个最简单的示例程序)
进入lib目录,复制该目录下的所有jar文件到 WebRoot/WEB-INF/lib目录中。
3. 修改配置文件
(1)web.xml—-定义核心Filter拦截用户请求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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>Struts2Simple</display-name> <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> <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> </web-app> |
(2)index.jsp—-根据需求编辑jsp请求代码,以及编写视图资源文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title><s:text name="loginPage"/></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="login"> <s:textfield name="username" key="user"/> <s:textfield name="password" key="pass"/> <s:submit key="login"/> </s:form> </body> </html> |
welcome.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:text name="succPage"/></title> </head> <body> <s:text name="succTip"> <s:param>${sessionScope.user}</s:param> </s:text><br> </body> </html> |
error.jsp:
1 2 3 4 5 6 7 8 9 10 |
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:text name="errorPage"/></title> </head> <body> <s:text name="failTip"/> </body> </html> |
(3)LoginAction.java—-定义处理用户请求的Action类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package cn.no7player.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ 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 String execute() throws Exception { if (getUsername().equals("zhoul")&& getPassword().equals("123456") ){ ActionContext.getContext().getSession().put("user" , getUsername()); return SUCCESS; } else{ return ERROR; } } } |
(4)struts.xml—-配置Action,配置处理结果和视图资源的关系:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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="default" namespace="/" extends="struts-default"> <action name="login" class="cn.no7player.struts2.action.LoginAction"> <!-- 定义三个逻辑视图和物理资源之间的映射 --> <result name="input">/login.jsp</result> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> </package> <!-- Add packages here --> </struts> |
(5)目录结构:
发表评论
难的是和自己保持联系~