Struts2的概述
1.什么是Struts2?
Struts2是一个基于MVC设计模式的WEB层框架。
Struts2的内核相对于Struts1来讲已经发生巨大变化。
2.常见的web层框架
Struts2 / Struts1 / Webwork / SpringMVC等
3.Web层框架基于前端控制器模型设计
Struts2的入门
1.下载Struts2的开发环境
http://struts.apache.org/
2.解压Struts2开发包
apps :Struts2提供的应用,war文件:web项目打成war包。直接放入到tomcat可以允许。
docs :Struts2的开发文档和API
lib :Strtus2框架的开发的jar包
src :Struts2的源码
3.创建web项目,引入jar包
引入jar包:struts-blank项目下找jar包
4.创建一个JSP页面 demo.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>Struts2的入门</title>
</head>
<body>
<h1>Struts2的入门</h1>
<h3><a href="${ pageContext.request.contextPath }/ hello.action">Struts2的入门</a></h3>
</body>
</html>
5.编写Action的类
package com.wangshi.struts.demo01;
/**
* @author wanghaichuan
*Struts2的入门HelloAction
*/
public class HelloAction {
/**提供一个方法
* 方法签名固定的
* 共有的 返回值是String类型的.方法名execute 在这个方法中不能传递参数;
*
*/
public String execute(){
System.out.println("HelloAction执行了......");
return "success";
}
}
6.对Action进行配置
在src下创建(提供)名称叫做struts.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- struts2的配置约束 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 修改常量 可从3个地方修改1-->
<!-- <constant name="struts.action.extension" value="whc"/> -->
<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
<!-- 配置Struts2的包 ================ -->
<package name="demo1" extends="struts-default" namespace="/">
<!-- 配置Action================ -->
<!-- 配置Action================ -->
<action name="hello" class="com.wangshi.struts.demo01.HelloAction" >
<!-- 配置页面的跳转=========== -->
<result name="success">/demo01/success.jsp</result>
</action>
</package>
</struts>
7.配置前端控制器(核心过滤器)
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_01</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>
<!-- 核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 修改常量 -->
<!-- <init-param>
<param-name>struts.action.extension</param-name>
<param-value>whc</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8.改写Action中的方法的返回值
package com.wangshi.struts.demo01;
/**
* @author wanghaichuan
*Struts2的入门HelloAction
*/
public class HelloAction {
/**提供一个方法
* 方法签名固定的
* 共有的 返回值是String类型的.方法名execute 在这个方法中不能传递参数;
*
*/
public String execute(){
System.out.println("HelloAction执行了......");
return "success";
}
}
9.改写struts.xml
10.编写success.jsp
Struts2的执行流程
当用户访问某一个Action的时候,先经过核心过滤器,在核心过滤器中执行一组拦截器(这组拦截器实现部分功能),执行目标Action,根据Action的返回值,进行页面跳转。
Struts2的常见配置
配置XML的提示
Struts2的配置文件加载顺序
init_DefaultProperties() ----加载default.properties
init_TraditionalXmlConfigurations(); ----加载struts-default.xml、struts-plugin.xml、struts.xml
init_LegacyStrutsProperties(); ----加载struts.properties
init_CustomConfigurationProviders(); ----加载配置提供类
init_FilterInitParameters() ; // [6] ----加载web.xml中过滤器初始化参数
init_AliasStandardObjects() ; // [7] ----加载Bean对象
加载顺序:
default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
注意:后配置的常量的值会覆盖先配置的常量的值。
Action的配置:
package相关配置
package标签称为包,这个包与Java中的包的概念不一致。包为了更好管理action的配置。
package标签的属性:
name :包的名称,只有在一个项目中不重名即可。
extends :继承哪个包,通常值为struts-default。
namespace :名称空间,与<action>标签中的name属性共同决定访问路径。
名称空间有三种写法:
带名称的名称空间 :namespace=”/aaa”
跟名称空间 :namespance=”/”
默认名称空间 :namespace=””
abstract :抽象的,用于其他包的继承。
action相关配置
action标签配置Action类。
action标签的属性
name :与namespace共同决定访问路径
class :Action类的全路径
method :执行Action中的哪个方法的方法名,默认值execute
converter :用于设置类型转换器
常量的配置
在Struts2的框架中,提供了非常多的常量:(在default.properties)
struts.i18n.encoding=UTF-8 ----Struts2中所有的post请求的中文乱码不用处理。
struts.action.extension=action,, ----Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写。
在Struts2中修改一些常量的值:
修改常量的值,可以有三个位置进行修正
- struts.xml中进行修改
- struts.properties中进行修改
- web.xml中进行修改
分模块开发的配置
include的配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- struts2的配置约束 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 修改常量 可从3个地方修改1-->
<!-- <constant name="struts.action.extension" value="whc"/> -->
<!-- 分模块开发的配置 -->
<include file="com/wangshi/struts/demo01/struts_demo01.xml" />
<include file="com/wangshi/struts/demo02/struts_demo01.xml" />
<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
<!-- 配置Struts2的包 ================ -->
<!-- <package name="demo1" extends="struts-default" namespace="/">
配置Action================
配置Action================
<action name="hello" class="com.wangshi.struts.demo01.HelloAction" >
配置页面的跳转===========
<result name="success">/demo01/success.jsp</result>
</action>
</package> -->
</struts>
三种写法
- Action类是POJO的类
- Action类实现一个Action的接口
package com.wangshi.struts.demo01;
import com.opensymphony.xwork2.Action;
/**
* @author wanghaichuan
*Action的编写方式二:实现一个Action的接口
*实现接口的这种方式:提供了5个常量(五个逻辑视图的名称)
*SUCCESS 成功
*ERROR 失败
*LOGIN 登录出错页面跳转
*INPUT 表单校验的时候出错
*NONE 不跳转
*
*/
public class ActionDemo02 implements Action{
@Override
public String execute() throws Exception {
System.out.println("Action02执行了");
return NONE;
}
}
Action类继承ActionSupport类
package com.wangshi.struts.demo01;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author WANGHAICHUAN
* Action的编写方式三:Action类继承ActionSupport类
* 推荐使用继承 ActionSupport方式
* ActionSupport提供了数据校验/国际化等一系列操作的方法
*
*/
public class ActionDemo03 extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("Action03的执行了......");
return NONE;
}
}
Action的访问:
界面
<%@ 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>Insert title here</title>
</head>
<body>
<h1>Action的访问</h1>
<h3>通过method方式访问</h3>
<a href="${pageContext.request.contextPath }/userFind.action">查询用户</a><br/>
<a href="${pageContext.request.contextPath }/userUpdate.action">修改用户</a><br/>
<a href="${pageContext.request.contextPath }/userDelete.action">删除用户</a><br/>
<a href="${pageContext.request.contextPath }/userSave.action">保存用户</a><br/>
<h3>通过通配符方式访问</h3>
<a href="${pageContext.request.contextPath }/product_find.action">查询商品</a><br/>
<a href="${pageContext.request.contextPath }/product_update.action">修改商品</a><br/>
<a href="${pageContext.request.contextPath }/product_delete.action">删除商品</a><br/>
<a href="${pageContext.request.contextPath }/product_save.action">保存商品</a><br/>
<h3>通过动态方法访问</h3>
<a href="${pageContext.request.contextPath }/customer !find.action">查询客户</a><br/>
<a href="${pageContext.request.contextPath }/customer !update.action">修改客户</a><br/>
<a href="${pageContext.request.contextPath }/customer !delete.action">删除客户</a><br/>
<a href="${pageContext.request.contextPath }/customer !save.action">保存客户</a><br/>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!-- struts2的配置约束 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 修改常量 可从3个地方修改1-->
<!-- <constant name="struts.action.extension" value="whc"/> -->
<!-- 把动态方式打开 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- Struts2为了管理Action的配置,通过包进行管理。 -->
<!-- 配置Struts2的包 ================ -->
<package name="demo2" extends="struts-default" namespace="/">
<!-- 通过method方式访问 -->
<action name="userFind" class="com.wangshi.struts.demo02.ActionDemo03" method="find" />
<action name="userUpdate" class="com.wangshi.struts.demo02.ActionDemo03" method="update" />
<action name="userDelete" class="com.wangshi.struts.demo02.ActionDemo03" method="delete" />
<action name="userSave" class="com.wangshi.struts.demo02.ActionDemo03" method="save" />
<!-- 通过通配符方式访问 第一个星表示管理 第二个星表示方法-->
<action name="product_*" class="com.wangshi.struts.demo02.ProductAction" method="{1}" />
<!-- <action name="*_*" class="com.wangshi.struts.demo02.{1}" method="{2}" /> -->
<!-- 通过动态方法访问-->
<action name="customer" class="com.wangshi.struts.demo02.CustomerAction" />
</package>
</struts>
- 通过method设置
user类
package com.wangshi.struts.demo02;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author WANGHAICHUAN
* Action的编写方式三:Action类继承ActionSupport类
* 推荐使用继承 ActionSupport方式
* ActionSupport提供了数据校验/国际化等一系列操作的方法
*
*/
public class ActionDemo03 extends ActionSupport {
public String find() {
System.out.println("查询用户执行了......");
return NONE;
}
public String update() {
System.out.println("修改用户执行了......");
return NONE;
}
public String delete() {
System.out.println("删除用户执行了......");
return NONE;
}
public String save() {
System.out.println("保存用户执行了......");
return NONE;
}
}
- 通过通配符的方式进行配置(*****)
package com.wangshi.struts.demo02;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author WANGHAICHUAN
*通配符方式
*/
public class ProductAction extends ActionSupport {
public String find() {
System.out.println("查询商品......");
return NONE;
}
public String update() {
System.out.println("修改商品......");
return NONE;
}
public String delete() {
System.out.println("删除商品......");
return NONE;
}
public String save() {
System.out.println("保存商品......");
return NONE;
}
}
- 动态方法访问
package com.wangshi.struts.demo02;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author WANGHAICHUAN
*动态
*/
public class CustomerAction extends ActionSupport {
public String find() {
System.out.println("查询客户......");
return NONE;
}
public String update() {
System.out.println("修改客户......");
return NONE;
}
public String delete() {
System.out.println("删除客户......");
return NONE;
}
public String save() {
System.out.println("保存客户......");
return NONE;
}
}
注意: