struts2
2015-12-24 22:55
561 查看
基于WebWork封装成的框架
特点:
1 只有Action组件,没有ActionForm组件。Action组件承担属性信息,并且处理方法。
2 Action非常灵活,可以使用一个普通的CLASS来充当
3 Action可测性强,Action的execute方法返回String 参数为无参
4 提供了result组件(json stream xmlHttpRequest ...),简化响应处理的实现
5 提供了大量拦截器组件
主要构成,看看STRUTS2的MVC实现是怎样的:
控制器: FilterDispatcher, StrutsPrepareAndExecterFilter
视图: 各种Result 支持视图组件响应 具体见图一中的类型
struts2标签 提供了分支,循环,显示等功能
模型: 普通的POJO(Action) 更加灵活
ValueStack组件 用于封装请求相关信息
使用总结:
1 创建web project,引入struts2开发基础包
2 在web.xml中配置控制器,添加控制器的配置文件struts.xml
3 编写jsp,Action组件
4 编写struts.xml配置,定义Action组件和请求的对应
struts的配置文件:
struts-default.xml(框架自带,定了一些Result组件)
struts.xml(开发者创建并使用,定义开发者编写的Result组件,拦截器组件,Action组件)
default.properties(框架自带,定义了框架的一些系统参数)
struts.properties(开发者创建并使用,用于覆盖default.properties中的配置。)
struts-plugin.xml(用于整合插件的配置)
struts2中的Action相关配置
1<package>元素的namespace属性 用于指定package中action对应的请求路径。
2<action>中的method属性 用于指定action中的业务方法名,默认为execute。
3<param>元素 用于为属性指定值,它包在action里面就是为action指定值,在result里面,就是为result指定值。
4通配符的使用
<action name="opt_*" method='{1}' class="he.action.OptAction">
<result name='success' type='dispatcher'>/ok.jsp</result>
<result name='fail' type='dispatcher'>/fail.jsp</result>
</action>
5 动态方法调用
<a href='opt!update'>update</a><br>
<a href='opt!delete'>delete</a><br>
<a href='opt!add'>add</a><br>
常使用到的API
ValueStack ActionContext ServletActionContext
ValueStack主要存储请求信息,内部结构分为root栈(Action压到栈底)和Context区域(Map结构,也叫变量存储区域key和value对应)
ActionContext提供了一些方法,ActionContext.getContext().get("application");以此获取context区域的信息。返回Map<key,value>
HttpServletRequest request = ServletActionContext.getRequest();返回原始对象。
struts.xml文件实例,注意里面的一些技巧:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package namespace="/" name="struts2-demo1" extends="struts-default">
<!-- 跳转到login.jsp页面 -->
<action name="toLogin">
<result >/WEB-INF/index.jsp</result>
</action>
<!-- 跳转到opt.jsp页面 -->
<action name="toOpt">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 登录示例 -->
<action name="login" class="tarena.action.Login" method="check">
<!-- 为Login对象中的dir属性赋值 -->
<param name="dir">D:\\images</param>
<!-- <result>/ok.jsp</result> -->
<result name="success" type="dispatcher">
<param name="location">/WEB-INF/ok.jsp</param>
</result>
<result name="login">/WEB-INF/index.jsp</result>
</action>
<!-- 通配符使用示例 -->
<action name="opt_*" method="{1}" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 动态方法调用示例 -->
<action name="opt" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>
</package>
</struts>
下面是一个简单的例子:
index.jsp
特点:
1 只有Action组件,没有ActionForm组件。Action组件承担属性信息,并且处理方法。
2 Action非常灵活,可以使用一个普通的CLASS来充当
3 Action可测性强,Action的execute方法返回String 参数为无参
4 提供了result组件(json stream xmlHttpRequest ...),简化响应处理的实现
5 提供了大量拦截器组件
主要构成,看看STRUTS2的MVC实现是怎样的:
控制器: FilterDispatcher, StrutsPrepareAndExecterFilter
视图: 各种Result 支持视图组件响应 具体见图一中的类型
struts2标签 提供了分支,循环,显示等功能
模型: 普通的POJO(Action) 更加灵活
ValueStack组件 用于封装请求相关信息
使用总结:
1 创建web project,引入struts2开发基础包
2 在web.xml中配置控制器,添加控制器的配置文件struts.xml
3 编写jsp,Action组件
4 编写struts.xml配置,定义Action组件和请求的对应
struts的配置文件:
struts-default.xml(框架自带,定了一些Result组件)
struts.xml(开发者创建并使用,定义开发者编写的Result组件,拦截器组件,Action组件)
default.properties(框架自带,定义了框架的一些系统参数)
struts.properties(开发者创建并使用,用于覆盖default.properties中的配置。)
struts-plugin.xml(用于整合插件的配置)
struts2中的Action相关配置
1<package>元素的namespace属性 用于指定package中action对应的请求路径。
2<action>中的method属性 用于指定action中的业务方法名,默认为execute。
3<param>元素 用于为属性指定值,它包在action里面就是为action指定值,在result里面,就是为result指定值。
4通配符的使用
<action name="opt_*" method='{1}' class="he.action.OptAction">
<result name='success' type='dispatcher'>/ok.jsp</result>
<result name='fail' type='dispatcher'>/fail.jsp</result>
</action>
5 动态方法调用
<a href='opt!update'>update</a><br>
<a href='opt!delete'>delete</a><br>
<a href='opt!add'>add</a><br>
常使用到的API
ValueStack ActionContext ServletActionContext
ValueStack主要存储请求信息,内部结构分为root栈(Action压到栈底)和Context区域(Map结构,也叫变量存储区域key和value对应)
ActionContext提供了一些方法,ActionContext.getContext().get("application");以此获取context区域的信息。返回Map<key,value>
HttpServletRequest request = ServletActionContext.getRequest();返回原始对象。
struts.xml文件实例,注意里面的一些技巧:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package namespace="/" name="struts2-demo1" extends="struts-default">
<!-- 跳转到login.jsp页面 -->
<action name="toLogin">
<result >/WEB-INF/index.jsp</result>
</action>
<!-- 跳转到opt.jsp页面 -->
<action name="toOpt">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 登录示例 -->
<action name="login" class="tarena.action.Login" method="check">
<!-- 为Login对象中的dir属性赋值 -->
<param name="dir">D:\\images</param>
<!-- <result>/ok.jsp</result> -->
<result name="success" type="dispatcher">
<param name="location">/WEB-INF/ok.jsp</param>
</result>
<result name="login">/WEB-INF/index.jsp</result>
</action>
<!-- 通配符使用示例 -->
<action name="opt_*" method="{1}" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>
<!-- 动态方法调用示例 -->
<action name="opt" class="tarena.action.OptAction">
<result>/WEB-INF/opt.jsp</result>
</action>
</package>
</struts>
下面是一个简单的例子:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action='login' method='post'> username:<input type="text" name='name' id='name' /><br> password:<input type='password' name='pwd' id='pwd'/><br/> <input type='submit' value='OK' /> </form> </body> </html>
index.jsp
相关文章推荐
- Java Unicode 编码互转
- Java API —— 异常
- 基于Spring Boot构建的Spring MVC快速入门
- Intellij idea 配置JDK
- Java NIO1:I/O模型概述
- java线程池使用
- 从头认识java-14.4 Java提供的数组的实用功能(2)
- myeclipse 10和myeclipse2015软件和破解工具
- Java的单例模式
- 为javasript中对象与json串之间转换的总结。
- java 读取 excel
- Java中Runnable和Thread的区别
- JDK,JRE,JVM,SDK,API,ADT,OOM,ANR
- 8个超有用的Java測试工具和框架
- 关于 java 显示GIF
- JAVA面向对象之继承
- ubuntu 安装JDK
- java 排序的实现方法
- 死磕Spring系列之三,XML解析相关
- java过滤特殊字符串