什么是Struts2?
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求。
(Struts前身webWork)
Struts2框架的控制器将“获取请求”和“分发转向”代码抽取出来写在配置文件里,这样一来,控制器(action类)就能专注于业务逻辑的处理了。
Struts2 的历史
Struts1 基于servlet
Webwork Struts2 基于filter
Servlet缺点:
1、接受参数比较麻烦(getParamater)
2、不进行参数的校验
3、servlet是一个线程不安全的
4、servlet具有容器依赖性,不利于单元测试
5、 servlet处理的请求很局限
6、…..
Struts2的好处(优点)
- 自动封装参数
- 参数校验
- 线程安全
- 结果的处理(转发|重定向)
- 国际化
- 显示等待页面
- 表单的防止重复提交
- 提供良好的Ajax 支持
- Struts2框架作用
替代servlet,作用接受客户端请求,并响应给客户端数据
4、Struts2 框架的搭建
- 导包
使用maven
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
</dependencies>
2)准备action类
public class HelloAction {
public String hello(){
System.out.println("hello struts2");
return "success";
}
}
3)书写核心配置文件struts.xml
<struts>
<package name="hello" namespace="/" extends="struts-default">
<action name="helloAction" class="com.fdw.action.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
- 配置文件详解
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--package
name:包的名字 随意起,但是其他包的名字不能重复
namespace:命名空间 访问地址的前缀
extends :继承 必须继承 struts-default
abstract:抽象 等待被继承 起一个标识作用
-->
<package name="hello" namespace="/" extends="struts-default">
<!--action
name :给action起个名字 随意起,是外部访问路径名,尽量与action类名一致
class : 类的完整路径名
method : 方法名 决定着访问类中的方法
-->
<action name="helloAction" class="com.fdw.action.HelloAction" method="hello">
<!--result
name :与访问的方法的返回值一致
type :转发 重定向(redirect) 默认的为:转发(dispatcher)
标签中值 :要跳转的页面
-->
<result name="success">/hello.jsp</result>
</action>
</package>
<!--引入其他配置文件-->
<include file="struts2.xml"></include>
</struts>
Struts常量配置
struts.devMode = false
指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
struts.enable.DynamicMethodInvocation = false
如果希望禁用隐式动态方法调用,请将其设置为false
struts.action.extension=action,,
指定反问action时的后缀名
http://localhost:8080/HelloAction.action
struts.i18n.encoding=UTF-8
i18n:国际化. 解决post提交乱码
修改常量
- 直接在struts.xml 修改
<constant name="struts.action.extension" value="action"></constant>
- 在 web.xml 配置
<context-param>
<param-name>struts.action.extension</param-name>
<param-value>do</param-value>
</context-param>
- 在 src下 或者resources下 创建一个Struts.properties
Struts2的高级配置
- 动态方法的开启
- *号占位符
<action name="helloAction_*" class="com.fdw.action.HelloAction" method="{1}">
- 开启常量
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
Method·不写,然后地址栏名和方法名用!隔开
注意 :高版本的Struts12中必须写<allowed-methods>才能使用
<allowed-methods>hello,dynamic</allowed-methods>
- 属性的默认值
!-- method属性:execute -->
<!-- result的name属性:success -->
<!-- result的type属性:dispatcher 转发 -->
<!-- class属性:com.opensymphony.xwork2.ActionSupport -->
默认访问的action
<default-action-ref name="defaultAction"></default-action-ref>
- action类的创建
- pojo 类
- 继承ActionSupport类,比接口多了一些action的优化 (推荐)
- 实现接Action接口
- Struts2 接受参数
Struts2 是一个线程安全
他的生命周期每次请求到来,都会重新创建action
接受参数的方式:
- 属性驱动
在action类中创建属性,属性的名字必须和参数的名字保持一致
自动接收参数
并给该属性创建对应的get和set方法
- 对象驱动
属性中获得get-set方法
在传参数过程,必须使用对象导航的方式
对象user 参数 user.name 这样属性会自动封装到对象user中
- 模型驱动
实现modelDrivern<Object>接口 泛型传递要转换的对象类型
实现其方法 方法中有个返回值 返回对应的属性
在action中定义一个属性,并new一个对象
总结:模型驱动不能转化集合,对象驱动可以转化集合
响应请求
- 转发
- 重定向
- 转到自己的action(xml action的配置)
<result name="success" type="chain">
<param name="actionName">toAction_add</param>
<param name="namespace">/</param>
<param name="name">${name}</param>
</result>
- 重定向到自己写的action(xml action的配置)
<result name="success" type="redirectAction">
<param name="actionName">toAction_add</param>
<param name="namespace">/</param>
<param name="name">${name}</param>
</result>
Ajax请求
- Stream流的方式
在action类中 添加属性 inputStream 给其添加对应的get-set方法
将json字符串转化为 字节流赋值为 inputStream
public class streamAction extends ActionSupport {
private InputStream inputStream;
@Override
public String execute() throws Exception {
JSONObject json = new JSONObject();
json.put("code",0);
json.put("msg","访问成功");
String str = json.toJSONString();
inputStream = new ByteArrayInputStream(str.getBytes("GBK"));
return super.execute();
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
并且在配置文件中 type 为 stream
加一个 响应的参数 就是这个属性
<action name="streamAction_*" class="com.fdw.ajax.streamAction" method="{1}">
<result name="success" type="stream">
<param name="inputStream">inputStream</param>
</result>
</action>