Struts2概述
struts2是一个mvc框架,重点在控制器上
struts1 + xwork = struts2
背景:struts2希望解决什么问题?
在web开发中,不同的功能采用不同的servlet,造成维护和管理的不便;struts2通过过滤器,将不同的操作引导到不同的方法执行中去
Struts2入门案例
第一步:导入Struts2版本对应需要的jar包。
第二步:创建action,action是一个类,每次访问action 的时候,默认会执行execute()方法,也可以指定要执行的方法。
package com.bj169.action;
/**
* @ClassName HelloAction
* @Description TODO
* @Author Administrator
* @Date 2018/11/28 0028 11:01
* @Version 1.0
**/
public class HelloAction {
public String execute() {
return "success";
}
public String hello() {
return "hello";
}
}
第三步:配置action的访问路径,需要创建struts2核心配置文件,需要注意:
核心配置文件名称和位置是固定的
名称: struts.xml,需要引入dtd约束:
位置: /src下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--包名,以及访问前缀,extends后面内容是固定的-->
<package name="dupei" extends="struts-default" namespace="/ehome">
<global-allowed-methods>regex:.*</global-allowed-methods>
<!--访问的名称,action类的位置,以及类中的方法名-->
<action name="hello" class="com.bj169.action.HelloAction" method="hello">
<!--//方法的返回值匹配,以及转发还是从定向方式-->
<result name="hello" type="redirect">/hello.jsp
</result>
</action>
<action name="*_User" class="com.bj169.action.HelloActionExtend" method="{1}"></action>
</package>
</struts>
action的配置(核心标签):
package:
name属性: 包的名称,用于区分Action
extends : 标准声明,继承struts-default
namespace:命名空间,用于声明访问项目的根路径
action:
name属性:action的访问名称
class: action类的全路径
method:执行action中的指定方法(默认执行execute)
result:
name: 和方法的返回值对应
type: 指定页面请求的方式
dispatcher(默认, 转发)
redirect: 重定向(到jsp)
redirect-action: 重定向(到action)
chain: 访问链
第四步:在web.xml中配置过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在struts2中配置修改常量
在struts.xml中修改(常用)
<!--设置字符集-->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!--设置为开发者模式,可以打印出更详细的错误信息-->
<constant name="struts.devMode" value="true"></constant>
Struts2的分模块开发
使用include标签,在struts.xml文件中引入其它的配置文件
<include file="hello.xml"></include>
Action的三种编写方式
- 声明一个普通action类,不继承任何类也不实现任何接口
- 创建类,实现Action接口
- 创建类,继承ActionSupport(主要)
访问action的方法 (3种)
1.使用action标签的method属性,通过设置,指定要执行的action
<!--访问的名称,action类的位置,以及类中的方法名-->
<action name="hello" class="com.bj169.action.HelloAction" method="hello">
2.使用通配符,在action标签中,用 * 来设置action类中方法的name属性,但struts2 2.5需要在package中配置
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="*_User" class="com.bj169.action.HelloActionExtend" method="{1}"></action>
package com.bj169.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* @ClassName HelloActionExtend
* @Description TODO
* @Author Administrator
* @Date 2018/11/28 0028 15:23
* @Version 1.0
**/
public class HelloActionExtend extends ActionSupport {
public String excute() {
return SUCCESS;
}
public String addUser() {
System.out.println("添加用户");
return NONE;
}
public String deleteUser() {
System.out.println("删除用户");
return NONE;
}
}
访问方式: http://localhost:8080/ehome/addUser_User.action 表示访问action类中的addUser()方法。
3.动态访问方式(少用)
在action方法中存在返回值,如果有返回值则必须是String,也可以没有返回值,如果指定execute方法返回值为void,或者返回NONE,在action标签中就不需要配置result标签。