Struts2 概述
由 lxjazni_7 创建,youj 最后一次修改 2016-12-12
Struts2 是目前较为普及和成熟的基于MVC设计模式的web应用程序框架,它不仅仅是Struts1 的升级版本,更是一个全新的Struts架构。最初,是以WebWork框架和Struts框架为基础,通过提供增强和改进的Struts框架,进而实现简化web技术人员开发工作的目标。不久之后,Webwork框架和Struts社区联合创造了现在流行的Struts2框架。
Struts2 框架的优点
了解了这几个主要的优点,会促使你考虑使用Struts2 :
POJO表单及POJO操作 - Struts2 去除掉了Struts框架中的Action Forms部分。在Struts2框架下,你可以用任何一POJO来接收表单输入,同样的,你可以把任一POJO视为一个Action类。
标签支持 - Struts2 改进了标签表单,而新的标签可让开发人员减少代码编写量。
AJAX支持 - Struts2 被认可接收进Web 2.0技术,并创建了功能非常类似于标准的Struts2 标签的AJAX标签,把AJAX支持整合进其结果中。
易于整合 - Struts有多种整合方式可使用,现在与其他类型的框架,如Spring、Tiles、SiteMesh之类的,整合更为容易了。
模板支持 - 支持使用模板生成视图。
插件支持 - 有大量的插件可用于Struts2,而使用插件可以增强和扩大Struts2 核心行为。
性能分析 - Struts2 为调试和配置应用程序提供综合的性能分析,此外,Struts也以嵌入调试工具的形式提供集成调试。
易于修改标签 - 在Struts2 中,可使用Freemarker的模板对标签标记进行调整,而修改标签不需要JSP或是Java知识,基本的HTML、XML和CSS知识就足够了。
促进减少配置 - Struts2 使用各种设置的默认值促进减少配置,而你不需要再配置什么除非是偏离了Struts2 设定的默认设置。
视图技术 - Struts2 为多种视图选项(JSP、Freemarker、Velocity、XSLT等)提供支持。
以上是使Struts2 成为准企业框架的十大优点。
Struts2 框架的缺点
尽管Struts2 有一大列的优点,但我们还是要提到关于它的一些仍需不断改进的缺点:
更大的学习曲线 - 使用Struts MVC,你必须要熟悉JSP、Servlet APIs标准以及一个大型、复杂的框架。
文档缺乏 - 相比于Servlet和JSP APIs标准,Struts的在线资源较少,许多初学者会发现Apache在线文档混乱并缺乏整理。
不够透明 - 相比于使用正常的基于Java的Web应用程序,使用Struts的应用程序有许多是进行在后台,这使得框架不易于理解。
最后说明一点,一个好的框架应该提供各种类型的应用程序都可以使用的通用行为,Struts2 是最好的Web框架之一,并频繁用于RIA(Rich Internet Applications)的发展。
1.环境搭建
导jar包
分模块开发的xml
基础模块
<?xml version="1.0" encoding="UTF-8"?><package name="base" extends="struts-default" abstract="true">
<!-- struts2.5之后动态调用需要匹配正则 -->
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
单一模块
<?xml version="1.0" encoding="UTF-8"?> /success.jsp /success.jsp<action name="paramAction" class="com.struts.one.web.ParamAction"></action>
总模块
<?xml version="1.0" encoding="UTF-8"?>xml 中央控制器
<?xml version="1.0" encoding="UTF-8"?> web_struts struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /*动态方法调用 开发中少,安全性低,基本没人用,不需要请忽略
下面为单一模块的配置文件
代码展示(//ServletActionContext.getRequest().setAttribute(“rs”, Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2())); 部分)
包括值栈传值(this.res = Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()); 部分)
public class CalAction implements ModelDriven{
//传统作用域向jsp传值
private Cal cal = new Cal();
//值栈传值(提供变量)
private Object res;
public Object getRes() {
return res;
}
public void setRes(Object res) {
this.res = res;
}
public String add() {
//ServletActionContext.getRequest().setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));
//值栈传值 = 传一个变量
this.res = Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2());
return "success";
}
public String min() {
//ServletActionContext.getRequest().setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));
this.res = Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2());
return "success";
}
public String mul() {
//ServletActionContext.getRequest().setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));
this.res = Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2());
return "success";
}
public String div() {
//ServletActionContext.getRequest().setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));
this.res = Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2());
return "success";
}
@Override
public Cal getModel() {
return cal;
}
}
2.与j2ee交互(传参)
一、浏览器->后台 主要讲解
1、modeldriven接口传值
2、set、get方法传值
3、对象导航传值
二、后台->浏览器jsp
1、传统作用域向jsp传值
—与j2ee容器交互—
1 非注入
1.1 耦合
ServletActionContext
1.2 解耦(建立使用解耦模式)
ActionContext
2 注入
2.1 耦合
2.2 解耦
三、值栈传值(具体看上面代码,传变量的)
代码展示
public class ParamAction implements ModelDriven{
/**
* 1、modeldriven接口传值
*/
private User user1 = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
/**
* 2、set、get方法传值
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 验证参数赋值问题
*/
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
/**
* 3、对象导航传值
*/
private User user2;
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String execute() {
System.out.println("modeldriven接口传值:"+this.user1);
System.out.println("set、get方法传值:"+this.name);
//System.out.println("set、get方法传值:"+this.uname);
System.out.println("对象导航传值:"+this.user2);
return null;
}
}