前端控制器模型
解压struts-2.3.24-all
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>
package com.itzhouq.struts.demo1;
/**
* Struts2的入门的Action类
*
*/
public class HelloAction {
/**
* 提供一个方法:
* *方法签名固定的,公有的,返回值是String类型,方法名为execute,在这个方法中不能传递参数
*/
public String execute() {
System.out.println("HelloAction执行了。。。。");
return null;
}
}
<?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="demo1" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="hello" class="com.itzhouq.struts.demo1.HelloAction"/>
</package>
</struts>
<!-- 配置Struts2的核心控制器 -->
<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>
package com.itzhouq.struts.demo1;
/**
* Struts2的入门的Action类
*
*/
public class HelloAction {
/**
* 提供一个方法:
* *方法签名固定的,公有的,返回值是String类型,方法名为execute,在这个方法中不能传递参数
*/
public String execute() {
System.out.println("HelloAction执行了。。。。");
return "success";
}
}
<struts>
<!-- Struts2为了管理Action的配置,通过包进行管理 -->
<!-- 配置Struts2的包=========== -->
<package name="demo1" extends="struts-default" namespace="/">
<!-- 配置Action===== -->
<action name="hello" class="com.itzhouq.struts.demo1.HelloAction">
<!-- 配置页面的跳转 -->
<result name="success">/demo1/success.jsp</result>
</action>
</package>
</struts>
<body>
<h1>页面跳转成功!!!</h1>
</body>
HelloAction执行了。。。。
信息,页面跳转到了success.jsp
页面。default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
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=action
web.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 com.itzhouq.struts.demo2;
/**
* Action的编写方式:Action类是一个POJO类
* @author itzhouq
*
*/
public class ActionDemo1 {
public String execute() {
System.out.println("ActionDemo1执行了.......");
return null;
}
}
package com.itzhouq.struts.demo2;
import com.opensymphony.xwork2.Action;
/**
* Action的编写方式二:实现一个Action接口
* * 实现接口的这种方式:提供了五个常量(五个逻辑视图的名称)
* *SUCCESS:成功
* *ERROR:失败
* *LOGIN:登录出错页面跳转
* *INPUT:表单校验的时候出错
* *NONE:不跳转
*
* @author itzhouq
*
*/
public class ActionDemo2 implements Action {
@Override
public String execute() throws Exception {
System.out.println("ActionDemo2执行了..........");
return null;
}
}
package com.itzhouq.struts.demo2;
import com.opensymphony.xwork2.ActionSupport;
/**
* Action的编写方式三:Action类继承ActionSupport类
* ***推荐使用继承ActionSupport的方式:
* ***ActionSupport提供了数据校验、国际化等一系列操作的方法。
* @author itzhouq
*
*/
public class ActionDemo3 extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("ActionDemo3执行了............");
return NONE;
}
}
三种写法在配置文件的配置信息:
<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