3.3Struct2(上午)的配置和在开发中的应用
来源:互联网 发布:mac xcode 玩游戏 编辑:程序博客网 时间:2024/05/16 18:07
(
struts的配置文件
action的类型
action中方法的调用
Action中如何获取Servlet的对象。
)
1、struts的配置文件
1.1struts的常量配置:
struts中默认位置:struts-core.jar中/org/apache/struts2/default.properties
struts中读取常量的4个地方:
A:struts-default.xml struts2-core-2.3.28.jar/struts-default.xml。一般不配置
B:struts.xml
C:struts.properties src目录下新建一个配置文件。 以key=value方式来配置
D:web.xml
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>false</param-value>
</init-param>
依次读取常量的配置。最后配置会覆盖前面配置的。
1.2include配置:导入另一个struts的配置文件。
多模块或者多个合作的项目。
<include file="struts-user.xml"></include>
<include file="struts-notice.xml"></include>
1.3package的配置
name: 包的名称。必须唯一。为其他package继承使用
namespace命名空间。客户端请求的的命名空间
extend 包之间的继承。
abstract true|falsetrue:表示包是一个抽象包,限制:不可以在抽象包中配置Action.extend 包之间的继承。
abstract true|falsetrue:表示包是一个抽象包,限制:不可以在抽象包中配置Action.
1.4action的配置
name:action的名称
class:action的class路径
method:调用action中的哪一个方法。如果不配置默认是execute方法。
param:在struts.xml中设置(SET)或者获取(GET)Actioin定义的成员变量。
设置:
获取:struts.xml是通过${成员变量名称},来获取成员变量的值。
result:配置action中返回的result
result的类型:
chain/dispatcher/redirect/redirectAction/stream/freemarker,默认为dispatch,表示默认是请求转发。
1:dispatch:请求转发到jsp页面
2:redirect:重定向到jsp页面
3:chain:请求转发到另一个Action
4:redirectAction:重定向另一个Action
5:stream:表示返回一个流(文件下载使用)
6:freemarker:表示返回的页面是一个freemarker页面。
2、struts的开发运用
2.1:action的类型
<a href="<%=path%>/type/typeAction_1">A:直接写class</a>
<a href="<%=path%>/type/typeAction_2">B:实现Action接口</a>
<a href="<%=path%>/type/typeAction_3">C:纪承ActionSupport类</a>
2.2:action中方法的调用,调用除了execute的方法
A:使用method属性。
<a href="<%=path%>/method/methodAction_1_add">add</a>
<a href="<%=path%>/method/methodAction_1_save">save</a>
<a href="<%=path%>/method/methodAction_1_update">update</a>
<a href="<%=path%>/method/methodAction_1_delete">delete</a>
B:使用动态方法调用:前提:开启struts.enable.DynamicMethodInvocation常量为true
调用方法:!+方法名称
<a href="<%=path%>/method/methodAction_2!add">add</a>
<a href="<%=path%>/method/methodAction_2!save">save</a>
<a href="<%=path%>/method/methodAction_2!update">update</a>
<a href="<%=path%>/method/methodAction_2!delete">delete</a>
C:使用通配符调用:对第一方法的简化操作。
<a href="<%=path%>/method/methodAction_3_add">add</a>
<a href="<%=path%>/method/methodAction_3_save">save</a>
<a href="<%=path%>/method/methodAction_3_update">update</a>
<a href="<%=path%>/method/methodAction_3_delete">delete</a>
Struts.xml中的配置:
<package name="meethod" namespace="/method" extends="struts-default"><action name="methodAction_1_add" class="com.action.method.MethodAction_1"method="add"></action><action name="methodAction_1_save" class="com.action.method.MethodAction_1"method="save"></action><action name="methodAction_1_update" class="com.action.method.MethodAction_1"method="update"></action><action name="methodAction_1_delete" class="com.action.method.MethodAction_1"method="delete"></action><action name="methodAction_2" class="com.action.method.MethodAction_2"></action><action name="methodAction_3_*" class="com.action.method.MethodAction_3"method="{1}"></action></package>
2.3:Action中如何获取Servlet的对象。
<a href="<%=path%>/servlet/servletAction_1">A:使用ServletActionContext这个类来获取</a>
<a href="<%=path%>/servlet/servletAction_2">B:让Action实现Servlet拦截器(struts框架提供)的接口</a>
A:
package com.action.servlet;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class ServletAction_1 extends ActionSupport {@Overridepublic String execute() throws Exception {/** * 1:使用ServletActionContext这个类来获取 */HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();ServletContext servletContext = ServletActionContext.getServletContext();HttpSession session = request.getSession();response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = response.getWriter();System.out.println(request.getContextPath());System.out.println(servletContext.getRealPath("/index.jsp"));System.out.println(session.getId());out.println("使用out往页面中往出内容");out.print("xxxxx");out.flush();out.close();return NONE;}}B:
package com.action.servlet;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class ServletAction_2 extends ActionSupport implementsServletRequestAware, ServletResponseAware, ServletContextAware {private HttpServletRequest request;private HttpServletResponse response;private ServletContext servletContext;private HttpSession session;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;if (this.request != null) {this.session = this.request.getSession();}}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;}@Overridepublic String execute() throws Exception {response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = response.getWriter();out.println(request.getContextPath() + "<br/><br/>");out.println(servletContext.getRealPath("/WEB-INF/lib/web.xml")+ "<br/><br/>");out.println("sessionID = " + session.getId() + "<br/><br/>");out.println("使用out往页面中往出内容" + "<br/><br/>");out.print("xxxxx" + "<br/><br/>");out.flush();out.close();return NONE;}}
通过以上两种方法,我们可以得出一个抽象的工具类,到时候所有的action类只要继承这个BaseAction类就好了。
package com.util;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public abstract class BaseAction extends ActionSupport implementsServletRequestAware, ServletResponseAware, ServletContextAware {protected HttpServletRequest request;protected HttpServletResponse response;protected ServletContext servletContext;protected HttpSession session;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;if (this.request != null) {this.session = this.request.getSession();}}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;}@Overridepublic abstract String execute() throws Exception;}
- 3.3Struct2(上午)的配置和在开发中的应用
- Struct2 的开发模式的配置问题
- Struct2应用开发步骤
- Struct2 配置 开发步骤
- struct2通配符的配置
- 关于struct2的配置问题
- struct2的配置及理解
- [记录]在struct2上配置json Plugin
- Struct1和Struct2的区别
- Struct2 配置
- 最近在整合struct2 和spring2.5.6
- struct2登录拦截器的配置
- Struct2获取response和request的方法
- jsp和struct2的路径问题
- MSMQ和.Net Remoting在MES开发中的应用
- MSMQ和.Net Remoting在MES开发中的应用
- javaweb_08-ServletConfig对象和它在开发中的应用场景
- 虚拟场景和用户故事在敏捷开发中的应用
- 阿里巴巴矢量图的使用
- HOG Matlab代码 3——关于 HOG 代码 的一些解释 1&2
- 文章标题
- Android 表格布局
- Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
- 3.3Struct2(上午)的配置和在开发中的应用
- 优化方法总结
- 根据2个经纬度点,计算这2个经纬度点之间的距离(通过经度纬度得到距离)
- 前端面试
- Servlet(动态网页)的学习
- 动态规划经典题目:最大连续子序列和
- 【差分约束系统】POJ 1201--Intervals
- javaWeb编程_Servlet01
- 极客公园 Mac 第三方客户端(swift)