Struts2学习笔记.doc_第1页
Struts2学习笔记.doc_第2页
Struts2学习笔记.doc_第3页
Struts2学习笔记.doc_第4页
Struts2学习笔记.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Struts1是一个优秀的框架,但是还是具有很多不好的地方,如Action和ActionForm严重倚赖系统框架中的API。如ActionForm必须继承ActionForm,Action控制器继承Action,测试麻烦。 Struts2是基于webwork发展而来,并非struts1使用MyEclipse8.5版本,可自动添加依赖struts2的系统库Struts2原理:说明:Struts2没使用Servlet作为核心控制器,而是换做一个Filter过滤器来进行,其类是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter过滤器所拦截的请求是/*(当然根据版本的不同,过滤器类也不同,如Org.apache.Struts2.dispatcher.FilterDispatcher)如果请求中带有.action或.do,则转入Struts2框架处理。配置文件:Web.xml中: struts2!-版本不同这里的配置不同-!Org.apache.Struts2.dispatcher.FilterDispatcher org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter configstruts-default.xml,struts-plugin.xml,./struts.xml struts2/*Struts.xml /index.jsp /fail.jsp 配置文件说明:1、 package中的name为了区分不同的包2、 extends是继承的包名3、 namespace是提交时区分的地址,如namespace=”user”,则提交时需要加入action=”/user/xxx.action”控制其中的代码,其中数据直接封装和页面表单元素名称对应。如果是对象,则页面表单元素名称写法为 对象.属性名LoginAction代码:public class LoginAction extends ActionSupport/ 封装用户请求参数的uidprivate String uid;private String pwd;public String getUid() return uid;public void setUid(String uid) this.uid = uid;public String getPwd() return pwd;public void setPwd(String pwd) this.pwd = pwd;private String shops;public String getShops() return shops;public void setShops(String shops) this.shops = shops;/ 这里直接返回字符串,和xml配置文件中的键值对应public String execute() throws Exception UserInfo userInfo = UserService.newInstance().login(this.uid, this.pwd);if (userInfo != null) /将用户放入session中ActionContext.getContext().getSession().put(currUser,u);String shops = ShopService.newInstance().getShopNames();this.setShops(shops);/以下是说明/当action设置了某个属性后,struts2将会将这些属性全部封装在一个Struts.valueStack的请求属性中。为了在JSP中输出商品信息,可以通过如下代码获取:/获得ValueStack 它类似于一个Map结构 这是JSP中代码/ValueStack vs = (ValueStack)request.getAttribute(“Struts.valueStack”);/String shops = (String)vs.findValue(“shops”);/并非通过Action中的属性名关联,而是通过getter和setter方法return cheng;/这里可以返回系统常量Return this.SUCCESS;return bai;/获得requestActionContext actx = ActionContext.getContext();/获得requestMap req = (Map)actx.get(request);HttpServletRequest request = ServletActionContext.getRequest();HttpSession session = ServletActionContext.getRequest().getSession();ServletContext ctx = ServletActionContext.getServletContext();/Map session = (Map)actx.getSession();/Map application = (Map)actx.getApplication();流程:1、 页面提交请求:action=” Login.action”2、 系统拦截.action后缀,找到对应的actionName,找到对应的Action处理3、 Action中可自动获取页面表单的数据,并采用execute方法处理4、 处理完毕后返回字符串,由字符串自动对应了Struts.xml中配置的result处理/index.jsp1、配置常量:在Struts.xml或者Sperties中更改为.do来处理请求或者:#将struts2业务逻辑控制器Action默认访问扩展名*.action修改为*.doStruts.action.extension = do解决中文乱码问题:修改为GBK时,相当于调用HttpServletRequest的setCharacterEncoding方法2、设置Struts2默认主题,解决页面布局混乱 配置Struts包配置在Struts2中配置包时,必须继承自Struts-default包,否则出错Namespace为命名空间配置那么页面提交时必须加入:测试Action中的操作1、 获取Request对象ActionContext ctx = ActionContext.getContext();Map request = (Map)ctx.get(request);2、 获取sessionActionContext ctx = ActionContext.getContext();Map session = (Map)ctx.getSession();3、 获取ServletContext对象ActionContext ctx = ActionContext.getContext();Map application = (Map)ctx.getApplication();其他获取方式:ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE)ActionContext.getContext().get(ServletActionContext.HTTP_CONTEXT)技巧:一般为了方便处理,会建立Action父类。BaseActionPublic class BaseAction/获取RequestPublic Map getRequest()Return (Map)ActionContext.getContext().get(“request”);/.获取session/.获取ServletContext/.获取ResponsePublic HttpServletResponse getResponse()HttpServletResponse response = ServletActionContext.getResponse();Response.setContentType(“text/html;charset=gbk”);Response.setCharacterEncoding(“gbk”);Return response;-关于公用同一个Action-方法一:提交代码如下:function submitInfo(type)var form = document.regForm;form.action = myReg!+type+.action;form.submit();对应的Action代码如下:public class RegAction extends BaseAction /定义注册方法public String reg() throws Exception/获取输出对象PrintWriter out = this.getResponse().getWriter();out.println(正在执行注册方法.);return null;public String check() throws ExceptionPrintWriter out = this.getResponse().getWriter();out.println(正在执行检查方法);return null;方法二:使用通配符配置,struts.xml中这里的1是占位符,对应前面name后面的*号,那么提交时需要按照指定格式进行:form.action = user_reg.action;form.action = user_check.action;Struts提供了一个ValueStack值栈,所有提交请求的数据先放入值栈中,然后再由值栈中放入对象或者属性中。-BaseAction-所有的Action中获取request、response很麻烦,于是可以开发一个BaseAction,如下:package com.scce.action;import com.opensymphony.xwork2.ActionContext;import java.util.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;public class BaseAction /获得request对象public Map getRequest()return (Map)ActionContext.getContext().get(request);/获得Session对象public Map getSession()return (Map)ActionContext.getContext().getSession();/获得ServletContext对象public Map getApplication()return (Map)ActionContext.getContext().getApplication();/获得Response对象public HttpServletResponse getResponse()HttpServletResponse response = ServletActionContext.getResponse();/设置相应头response.setContentType(text/html;charset=gbk);response.setCharacterEncoding(gbk);return response;-Action公用在Struts1中,默认方法为execute,Struts2中同样也是,这样问题来了,每个操作写个Action吗?struts1中是通过Action类继承DispatchAction,然后通过在表单或者超链接后传递一个配置的参数用于和方法对应。那么Struts中简单了,Action中写的方法,如checkLogin(),如addUser,deleteUser等等,则页面提交如下:用这种格式去区分方法2:使用通配符,如下: 为了规范Action类,Struts2提供了一个Action接口 public interface Actionpublic static final java.lang.String SUCCESS = success; public static final java.lang.String NONE = none; public static final java.lang.String ERROR = error; public static final java.lang.String INPUT = input; public static final java.lang.String LOGIN = login; /定义处理用户请求的execute方法public abstract java.lang.String execute() throws Exception;public class LoginAction implements Action . . . public String execute() throws Exception if(scott.equals(this.uid) & tiger.equals(this.pwd) return this.SUCCESS; return this.ERROR;-Action中的验证功能:ActionSupport是一个工具类,它已经实现了Action借口,还实现了Validatable接口,用于进行验证功能。通过重写该方法可以在校验表单输入出错时,将错误添加至ActionSupport类的fieldErrors域中,然后通过Struts2的标签输出错误提示。Action可以继承ActionSupport类,重写Validate()方法,如下:Public class LoginAction extends ActionSupportPrivate String uid;Private String pwd;Public String execute() throws ExceptionPublic void validate()If(uid.equals(“”)This.addFieldError(“uid”,”用户名不能为空”);Else if(pwd.equals(“”)This.addFieldError(“pwd”,”密码不能为空”);注:validate在execute方法之前执行页面上使用自动显示错误信息指向跳转还是重定向:/index.jsp/fail.jsp-拦截器-拦截器类必须实现借口:InterceptorVoid destroy();/执行一次Void init();/执行一次String intercept(ActionInvocation invocation) throws Exception;该方法是用户需要实现的拦截器动作,返回一个result配置字符串作为逻辑视图,该方法的ActionOnvocation参数包含被拦截的Action的引用,可以通过调用该参数的Invoke方法,将控制权转给拦截器或者Action的execute方法一般通过继承AbstractInterceptor类实现,如下:一、重写intercept拦截器拦截方法public class MyInterceptor extends AbstractInterceptor / 定义拦截器属性,指定被拦截的Action休眠的时间private int sleep;public int getSleep() return sleep;public void setSleep(int sleep) this.sleep = sleep;public String intercept(ActionInvocation invocation) throws Exception System.out.println(开始执行Action的时间: + (new Date().toLocaleString();/获得当前的Action(这里只是举例看看就行)/LoginAction ac = (LoginAction)invocation.getAction();/ 获得被拦截的ActionContext对象ActionContext ctx = invocation.getInvocationContext();/ 取得sessionHttpSession session = (HttpSession) ctx.getSession();/ 获得权限String role = (String) session.getAttribute(role);/ 获得requestMap request = (Map) ctx.get(request);if (role = null) / 说明未登录request.put(msg, 您还没有登录,请先登录!登录);return error; else / 已登录/ System.out.println(已登录.);/ 拦截Action执

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论