目录
1.Struts2概述 1.1 什么是Struts21.2 Web层框架基于前端控制器模型设计2. Struts2入门案例 2.1 Struts2的开发环境2.2 解压开发包2.3 创建项目,引入jar包2.4 创建一个JSP页面2.5 编写Action的类2.6 对Action进行配置2.7 配置前端控制器(核心过滤器)2.8 测试2.9 改写Action中的方法的返回值2.10 改写struts.xml2.11 编写success.jsp2.12 再次测试3. Struts2的执行流程4. Struts2的常见配置 4.1 XML的提示问题4.2 Struts2的配置文件的加载顺序(了解) 4.2.1 源码:Struts2的配置文件的加载顺序4.2.2 加载顺序4.3 Action的配置 4.3.1 package相关配置4.3.2 action相关配置4.4 Struts2中常量的配置4.5 分模块开发的配置5. Action的访问 5.1 Action的三种写法 5.1.1 方式一:Action类是POJO类5.1.2 方式二:Actions实现了一个Action的接口5.1.3 方式三:Action类继承ActionSupport类【推荐】5.2 Action的访问 5.2.1 通过method设置5.2.2 通过通配符的方式【掌握】5.2.3 动态方法访问前端控制器模型
解压struts-2.3.24-all
apps----------Struts2提供的应用,war文件:web项目打成war包。直接放入到tomcat可以允许。docs ----------Struts2的开发文档和APIlib--------------Strtus2框架的开发的jar包src-------------Struts2的源码
WebContent/demo1/demo1.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <h2>Struts2的入门</h2> <h3><a href="${pageContext.request.contextPath }/hello.action">Struts2的入门</a></h3> </body> </html>default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml 前三个是框架提供的配置文件,后三个都可以配置Struts2的常量注意:后配置的常量的值会覆盖先配置的常量的值。package标签称为包,这个包与Java中的包的概念不一致。这个包为了更好管理action的配置。
package标签的属性
name:包的名称,只有在一个项目中不重名即可。
extends:继承哪个包,通常值为struts-default。
namespace:名称空间,与<action>标签中的name属性共同决定访问路径。
名称空间有三种写法:
带名称的名称空间:namespace=”/aaa”
跟名称空间:namespance=”/”
默认名称空间:namespace=””
abstract :抽象的,用于其他包的继承.
在Struts2的框架中,提供了非常多的常量,主要在default.properties中
struts.i18n.encoding=UTF-8 ----Struts2中所有的post请求的中文乱码不用处理。
struts.action.extension=action,, ----Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写。
在Struts2中修改一些常量的值:可以与三个位置
struts.xml中进行修改
<!-- 配置Struts2的常量 --> <constant name="struts.action.extension" value="abc"/>struts.properties中进行修改
struts.action.extension=actionweb.xml中进行修改
<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>xyz</param-value> </init-param> </filter>一般在struts.xml中进行修改,如果在三个文件中都修改,则以web.xml中为准。
include的配置,引入多个struts.xml配置文件
<include file="com/itzhouq/struts/demo1/struts_demo1/xml"/>三种写法在配置文件的配置信息:
<package name="demo2" extends="struts-default" namespace="/"> <!-- 配置Action===== --> <action name="ActionDemo1" class="com.itzhouq.struts.demo2.ActionDemo1"></action> <action name="ActionDemo2" class="com.itzhouq.struts.demo2.ActionDemo2"></action> <action name="ActionDemo3" class="com.itzhouq.struts.demo2.ActionDemo3"></action> </package>首先写一个JSP页面../WebContent/demo3/demo3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Action的访问</h1> <h3>通过method方式</h3> <a href="${ pageContext.request.contextPath }/userFind.action">查询用户</a> <a href="${pageContext.request.contextPath }/userDelete.action">删除用户</a> <a href="${pageContext.request.contextPath }/userUpdate.action">修改用户</a> <a href="${pageContext.request.contextPath }/userSave.action">保存用户</a> </body> </html>编写Action类com.itzhouq.struts.demo3.UserAction
package com.itzhouq.struts.demo3; import com.opensymphony.xwork2.ActionSupport; /** * Action的访问方式:通过method方式 * @author itzhouq * */ public class UserAction extends ActionSupport { public String find() { System.out.println("查询用户......."); return NONE; } public String delete() { System.out.println("删除用户......."); return NONE; } public String update() { System.out.println("更新用户......."); return NONE; } public String save() { System.out.println("保存用户......."); return NONE; } }在和Action同一个包写编写配置问价com/itzhouq/struts/demo3/struts_demo3.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> <!-- Struts2为了管理Action的配置,通过包进行管理 --> <!-- 配置Struts2的包=========== --> <package name="demo3" extends="struts-default" namespace="/"> <!-- 配置Action===== --> <action name="userFind" class="com.itzhouq.struts.demo3.UserAction" method="find"></action> <action name="userDelete" class="com.itzhouq.struts.demo3.UserAction" method="delete"></action> <action name="userUpdate" class="com.itzhouq.struts.demo3.UserAction" method="update"></action> <action name="userSave" class="com.itzhouq.struts.demo3.UserAction" method="save"></action> </package> </struts>在主配置文件中引入struts_demo3.xml
<include file="com/itzhouq/struts/demo3/struts_demo3.xml"/>编写Action类ProductAction
package com.itzhouq.struts.demo3; import com.opensymphony.xwork2.ActionSupport; 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; } }在../struts_demo3.xml配置文件中添加
<!-- 通配符的方式 --> <action name="product_*" class="com.itzhouq.struts.demo3.ProductAction" method="{1}"></action>通配符的配置
开启动态方法访问,在struts_demo3.xml配置文件中添加
<!-- 开启动态方法访问 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>动态方法访问的配置
<!-- 动态方法访问的方式 --> <action name="customer" class="com.itzhouq.struts.demo3.CustomerAction"></action>编写访问路径jsp文件
<h3>通过动态方法访问的方式</h3> <a href="${ pageContext.request.contextPath }/customer!find.action">查询用户</a> <a href="${ pageContext.request.contextPath }/customer!update.action">查询用户</a> <a href="${ pageContext.request.contextPath }/customer!delete.action">查询用户</a> <a href="${ pageContext.request.contextPath }/customer!save.action">查询用户</a>转载于:https://www.cnblogs.com/itzhouq/p/Struts2.html