Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。
Struts2流程 :
1.客户端(Client)向Action发用一个请求(Request)
2.Container通过web.xml映射请求,并获得控制器(Controller)的名字
3.容器(Container)调用控制器(StrutsPrepareAndExecuteFilter或FilterDispatcher)。在Struts2.1以前调用FilterDispatcher,Struts2.1以后调用StrutsPrepareAndExecuteFilter
4. 控制器(Controller)通过ActionMapper获得Action的信息
5.控制器(Controller)调用ActionProxy
6.ActionProxy读取struts.xml文件获取action和interceptor stack的信息。
7.ActionProxy把request请求传递给ActionInvocation
8.ActionInvocation依次调用action和interceptor
9. 根据action的配置信息,产生result
10.Result信息返回给ActionInvocation
11.产生一个HttpServletResponse响应
12.产生的响应行为发送给客服端。
首先我们需要建一个web 工程。 导入需要的jar 包到lib目录下(注意包多,只需要导入我们需要多包,不然也会报错,这里用的是struts2_2.3.24)
下边看一下完整的项目目录结构。
如上有箭头的文件 例: XXXaction, XXXstruts.xml, XXXweb.xml, XXXLogli.jsp 是我们需要操作 的文件。没有需要新建。
有演示代码——具体类容如下。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
<display-name>StrutsProjict2</display-name>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?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>
<!-- namespace 命名空间 -->
<package name="default" namespace="/" extends="struts-default">
<!--默认jsp-->
<!-- <default-action-ref name="index"></default-action-ref>
<action name="index" >
<result >/MyError.jsp</result>
</action> -->
<!--method 方法 class action 所在的路径-->
<action name="helloword_*" method="{1}"
class="com.struts.action.HolleWordAction">
<result name="success">/MyJsp.jsp</result>
<result name="add">/{1}.jsp</result>
<result name="upDate">/{1}.jsp</result>
</action>
<!--input 当拼接的uil 错误时,再次返回的jsp-->
<action name="logln" class="com.struts.action.LogLnAction" method="logLi" >
<result name="success">/MySuccess.jsp</result>
<result name="input">/Logln.jsp</result>
</action>
</package>
<!-- 指定uil 后缀为XML-->
<!-- <constant name="struts.action.extension"value="xml"></constant> -->
</struts>
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%>
<%
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 'MyJsp.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>
我的jsp . <br>
</body>
</html>
HolleWordAction
package com.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class HolleWordAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行action");
return SUCCESS;
}
public String add() {
System.out.println("我是add");
return "add";
}
public String upDate() {
System.out.println("我是upDate");
return "upDate";
}
}
以上只是部分代码
struts.xml详解
http://www.cnblogs.com/wkrbky/p/5889328.html
Struts2框架中使用包来管理action,避免了Servlet在web.xml中难以管理的与维护的局面.包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action,在实际应用中,我们应该把一组业务功能相关的action 放在同一个包下.
配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,如果其他包要继承该包,必须通过该属性进行引用,包的namespace属性用于定义该包的命名空间,命名空间作用为访问该包下的action路径的一部分,见示例.namespace属性可以不配置,如果不指定该属性,默认的命名空间为””
通常每个包都应该继承struts-default包,因为struts2很多核心功能都是拦截来实现的,如,从请求中把请求参数封闭到action,文件上传和数据验证等都是通过拦截器实现的,struts-default定义了这些拦截器和Result类型,可以这么说,当包继承了struts-default才能使用struts2提供的核心功能,struts-default包是在struts2-core-2.xx.jar文件中的struts-defalut.xml中定义,struts-default.xml也是struts2默认配置文件,struts2每次都会自动加载struts-default.xml文件.
package还有一个abstract=”true”属性,指定此包为抽象包,和抽象类的概念差不多,说明此包只能被其他包继承,则它里面不允许包含action元素.
struts.xml文件的分离
我们的web.xml文件非常之大,到后来是越来越难的查找与维护,看得头都是大的,Struts2配置文件可以分离,很好的解决了此问题.
通过主次配置文件的分离,可以加强团队间的合作,并且互不打扰彼此的配置文件,出了问题也知道责任在哪里.
在实例开发中也是这样做的,通过一个主文件中,打开全局开关,引入其他子配置文件
ActionSupport类的作用
struts2不要求我们自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可以大大的简化Acion的开发。
Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。
默认值
class=”” ActionSupport
method=”” execute
name=”” “success”
Action接口里提供了一些常量及execute方法,通常我们自己写的Action可以实现这个接口, ActionSupport已经实现了这个接口,并且还实现了验证,国际化等功能的接口,所以我们自己写的Action类通常会继承ActionSupport这个类来达到启用验证框架,国际化,自动转换等功能的目的.
Action中5中内置属性
(1) SUCCESS :Action正确的执行完成,返回相应的视图,success是name属性的默认值。
(2) NONE :表示Action正确的执行完成,但并不返回任何事视图。
(3) ERROR : 表示Action执行失效,返回错误处理视图。
(4) LOGIN : Action因为用户没有登录的原因没有正确执行,将返回该登录视图,要求用户进行登录验证
(5) INPUT : Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入界面,一般在应用中,会对这些 参数进行验证,如果验证没有通过,将自动返回该视图。