struts2完全学习文档

编译问题:
java(jdk)编译设置:window-per**-java-instelled jar
java Compiler 编译设置与jdk的配置一样
Tomcat 使用的jdk与编译设置的jdk也应该是一致的

Acion的配置:
1.Struts的开发模式设置<constant name="struts.devMode" value="true"/>
2.Struts中web.xml的配置文件的设置:<filter>...</filter><filter-mapping>...</filter-mapping>
3.为jar文件建立源文件doc时,按f1可以导航到相关的文档
4.为配置文件设置提示 为 xml catalog 配置相关的dtd文件。
5.运行机制:客户端-tomcat-web.xml-filter-filter class-action-result
6.Namespace:
   (1)'/'->/index.action
   (2)'/**' ->/**/index.action
   (3)'/**/**' ->/**/**/index.action
   为对应的模块名。
  namespace 不写,将默认接受所有的action
  result若没有写名字,将默认为'success'.
  extends:
    从另外一个包来继承,声明一个主要配置,然后所有的都继承该包.
7.action返回:当struts.xml中没有调用方法时,默认调用execute()方法。
  struts.xml中的action中没有class是默认调用ActionSupport的execute();
  Action中有默认常量:SUCCESS ERROR INPUT等。
  可以继承implements Action / extends ActionSupport
8.Action方法调用:
  (1)struts.xml中的action的属性指定 method="方法名"
  (2)struts.xml中的action不配置属性method,而在url中使用 ‘!方法名’(dmi动态方法调用)来动态调用。
9.通配符的配置:
  使用“*”代表所有字符,使用“{1开始的数字调用第几个*}”
  使用法则:约定优于配置(提前把文件名约定好)
10.action接收参数三种方法:
  (1)(常用)通过Action属性来接收参数,属性必须写getter/setter方法。属性自动转换类型。调用时:url?属性=值&属性=值
  (2)(常用)通过DomainModel(域模型)接收参数,使用Model生成getter/setter,调用时:url?model.属性=值&model.属性=值
     --(上)通过Dto(数据传输对象)接收参数,使用同DomainModel
  (3)通过ModelDriven接收参数,在Action中 implements ModelDriven<Model>,然后又在Action中new 一个Model(必须自己new Model()),最后再继承 getModel()方法。
11.中文问题解决:
  struts.xml中使用i18n配置,<constant name="struts.i18n.encoding" value="字符编码"/>  --i18n internationalization 
  配置文件参考:struts-core.jar/org.apache.struts2/default.properties
12.简单数据校验
   在action中配置一个逻辑 然后调用 this.addFieldError("属性","错误信息");在Struts配置错误返回页面。
   页面使用struts标签<s:fielderror fieldName="属性名" theme="simple"/>
   打印错误信息:<s:property value="errors.name[0]"/> 错误信息保存在一个数组中。
   <s:debut></s:debug>显示调试信息
13.Action访问web元素 request,session,application,HttpRequest,HttpSession
   (1)request=(Map)ActionContext.getContext().get("request");
   session=ActionContext.getContext().getSession();
   application=ActionContext.getContext().getApplication();
    Action赋值:
   request.put("属性","值");
   session.put("属性","值");
   application.put("属性","值");
   jsp取值:
    <s:property value="#request.属性"/>
   (2)(常用)Action implements RequestAware,SessionAware,ApplicationAware (这也是IOC/DI的实现),定义Map<String,String> request session 和application.
   (3)在Action中定义
      private HttpServletRequest request;
      private HttpSession session;
      private ServletContext;
   Action取值:
      request= ServletActionContext.getRequest();
      session=request.getSession();
      application=session.getServletContext();             
   (4)Action implements ServletRequestAware
      private HttpServletRequest request;
      @override
      setServletRequest(**){request=request;session=request.getSession();}
14.模块包含
   <include file="其他的xml路径"/>
15.默认Action
   <default-action-ref name="默认Action名"/>

Result的配置:
1.类型type:
  (1)dispatcher 服务器端跳转到视图
  (2)redirect 客户端跳转
  (3)chain 服务器端跳转到Action和视图,<result type="chain">Action前面不要加"/",有参数param(actionName,namespace)两个属性(跳转到其他包)</result>;
  (4)RedirectAction跳转到其他Action, 地址栏显示跳转后的Action地址
2.全局结果集
  <global-results>
        <result name="全局返回类型">/other.jsp</result>
  </global-results>
3.向结果集传递参数
  (1)url?type=typeValue
  (2)Action中声名该属性type
  (3)result中取值<result>/url?type=${type}</result>
  (4)jsp页面取值:<s:property value="#parameters.type"/>O

本文由 程序员百味 http://www.bywei.cn/blog 编写

OGNL(对象Graph导航语言)表达式和struts标签(Action中演示变量:name属性,model实体类)
------->OGNL表达式
1.<s:property value="值棧中的属性名(name)"/>
2.<s:property value="model.name"/>
3.<s:property value="model1.model2.name"/>
4.<s:property value="name.length()"/> 调用普通方法
5.<s:property value="model.method()"/>调用普通方法
6.<s:property value="method()"/>调用普通方法
7.struts.xml配置:<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
  <s:property value="@cn.bywei.struts2.ognl.Class@staticMethod()"/> 访问值棧中的静态方法
  <s:property value="@cn.bywei.struts2.ognl.Class@name"/> 访问值棧中的静态属性
8.<s:property value="new cn.bywei.strus2.ognl.Class()"/>调用构造方法
9.访问集合:
  (1)<s:property value="List"/> 访问所有
     <s:property value="List[index]"/> 访问index的属性集合
     <s:property value="List{name}[index]"/> 访问属性集合中的一个
  (2)<s:property value="Set"/>
  (3)<s:property value="Map"/>
     <s:property value="Map['key']"/>  /   <s:property value="Map[\"key\"]"/>
     <s:property value="Map.keys"/>  <s:property value="Map.values"/>
     <s:property value="Map.size()"/>
10.过滤(投影)
<s:property value="model.{?#this.age==1}.{age}"/> age=q的model, this指定拿到的当前对象
<s:property value="model.{^#this.age>1}.{age}"/> age>1的开头的那个
<s:property value="model.{$#this.age>1}.{age}"/> age>1的结尾的那个
11.通过盏值访问
<s:property value="[index].name"/> 代表盏值中的index个对象的属性name
-------->struts2标签
12.<s:property value="值" default="默认值" escape="html标签格式化(true/false)"/>
   从request取值:<s:property value="#request.name"/>
   从Action中取值:<s:property value="#name"/>
13.<s:set value="值" var="变量" scope="默认为Action.(application,session,request,page,action)"/>
14.<s:bean name="类路径" var="变量">
        <s:param name="类中的属性" value="'设定新值'"/>
   </s:bean>
15.(少用)<s:include value="被包含的文件"/> 包含静态英文文件
   <s:set var="incpage" value="'/include.html'"/>
   <s:include value="%{#incpage}"/>
16.<s:fielderror fieldName="fielderror.text" theme="simple"/>
   其中的tehme设置:(1)css样式表解决 (2)xhtml解决(->struts-core.jar/template/fielderror.ftl -->struts.xml中配置:<constant name="struts.ui.theme" value="自定义的theme"/>)
17.<s:if test="表达式">
     <s:elseif test="表达式"></s:elseif>
     <s:else></s:else>
   </s:if>
18.<s:iterator value="集合可自定义map:#{1:'value1',2:'value2'}" var="变量" status="status(count,index,even(偶数),odd(奇数),first,last)"
19.UI标签(操作复杂,与javaScript结合困难,所以不常用)
   theme 主题标签

异常处理
1.try{}catch()finally{}  
2.声明式异常.通过throws()把异常往上抛,交给Struts2来处理
  -->struts.xml中配置:
     <result...../>
       <exception-mapping result="error" exception="java.sql.SQLException(异常类型)"/>
       <result name="error">/error.jsp</result>
3.全局异常映射
  -->struts.xml中配置:
   <global-results>
      <result name="errot" >/error.jsp</result>
   </global-results>
   <global-exceptiong-mappings>
       <exception-mapping result="error" exception="java.lang.Exception(错误类型)"></exception-mapping>
   </global-exception-mappings>

国际化
1.Local 获取本地信息,ResourceBundle 绑定本地properties文件
  建立message_zh_CN.properties
2.struts国际化
  (1)类级别properties文件必须和对应的类名相同(Class_zh_CN.properties)
     包级别properties文件在包下
     全局properties,必须在struts.xml中配置:<constant name="struts.custom.i18n.resources" value="配置文件名"/>
  (2)jsp页面获取:<s:property value="getText('配置文件对应的key')"/>
  (3)传递参数动态显示:
     --在properties文件中配置{0}占位符
     --jsp页面获取:
       <s:text name="配置文件中的key">
           <s:param value="值"/>
       </s:text>
动态语言切换:使用连接<a href=?request_loale=zh_CN >中文</a>

控制重复提交
1.在form表单中设置method="post"
2.在表单中设置<s:token></s:token>  token-令牌
  struts.xml 中配置:
      <interceptor-ref name="defaultStack"/>
      <interceptor-ref name="token"/>
      <result name="invalid.token">/errot.jsp</result>
类型转换
1.Action默认已经做了类型转换
2.日期转换:<s:date name="名字" format="yyy/MM/dd HH:mm:ss"/>
3.其他类型的转换,如:list,set,map以及自定义的类型转换少用

猜你喜欢

转载自wjheye.iteye.com/blog/1035939