您的位置:首页 > 编程语言 > Java开发

struts2学习

2016-01-03 20:21 295 查看


<struts> 

<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">

        

        <action name="hell">

            <result>

                /Hello.jsp

            </result>

        </action>

    </package>

</struts>

一、action

具体Action的实现可以 1、是一个普通的java类   2、或者实现Action接口  3、不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法

Action执行的时候并不一定要执行execute方法

可以在配置文件中配置Action的时候用method=来指定执行哪个方法

也可以在url地址中动态指定(动态方法调用DMI)(推荐)  

        String path = request.getContextPath();

        struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。

         虽然可以用redirect方式解决,但redirect方式并非必要。

        解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextPath方式来拿到webapp的路径)
<a href="<%=path %>/user/userAdd">添加用户
<a href="<%=path %>/user/user!add">添加用户

前者会产生太多的action,所以不推荐使用

二、action的搜索顺序

1、获得请求路径URL 例如 http://server/struts2/path1/path2/path3/test.action
2、首选寻找/path1/path2/path3的package寻找名字为test.action,如果不存在在执行步骤3;如果存在则执行5

3、寻找namespace为/path1/path2中package,如果不存在执行步骤4 ;如果存在则执行5

4、寻找namespace为/path1的package,如果不存在则执行步骤5;如果存在则执行5

5、如果存在就会去找对应的action,如果找不到则回去默认namespace的package里面寻找(默认的命名空间为空字串"") 如果还是找不到则页面提示找不到action

三、action的默认值

1、如果没有指定class 默认是ActionSupport

2、如果没有指定method 默认是action中的execute()方法

3、如果没有指定result的name属性默认是success

三、action配置通配符

        <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">

            <result>/Student{1}_success.jsp</result>

        </action>

        

        <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">

            <result>/{1}_{2}_success.jsp</result>

        </action>

四、action接收URL或者表单属性封装的三种方式

1、使用action属性接收参数 get和set方法

2、使用Domain Model接收参数

3、使用ModelDriven接收参数

参数中有中文  配置文件中加    <constant name="struts.i18n.encoding" value="GBK" /> <!-- internationalization 处理中文-->

 <constant name="struts.action.extension" value="do"></constant> 将默认的访问方式.action 改成.do

struts2加载常量的顺序

1、struts-default.xml

2、struts-plugin.xml

3、struts.xml

4、struts.properties

5、web.xml       如果多个文件都配置了同一个常量 后面会覆盖前面文件中配置的常量

取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用:

Action1、private Map request= (Map)ActionContext.getContext().get("request");
 private Map session = ActionContext.getContext().getSession();
 private Map application = ActionContext.getContext().getApplication();

Action2、extends ActionSupport implements RequestAware,SessionAware, ApplicationAware

Action3、private HttpServletRequest request =  ServletActionContext.getRequest();
 private HttpSession session = request.getSession();
 private ServletContext application = session.getServletContext();

Action4、extends ActionSupport implements ServletRequestAware

四、数据校验

使用addFieldError方法和s:fieldError标签简单处理数据校验

if(name == null || !name.equals("admin")) {
this.addFieldError("hh", "name is error");
this.addFieldError("hh", "name is too long");
return ERROR;


<s:fielderror fieldName="hh" theme="simple"/>
<s:property value="errors.hh[0]"/>

五、返回值类型

   <action name="r1">
    <result type="dispatcher">/r1.jsp</result>
   </action>
   
   <action name="r2">
    <result type="redirect">/r2.jsp</result>
   </action>
   
   <action name="r3">
    <result type="chain">r1</result>
   </action>
   
   <action name="r4">
    <result type="redirectAction">r2</result>
   </action>

六、类型转换器



继承 StrutsTypeConverter 重写convertFromString方法 

1、局部转换器 名字必须和action名保持一致

p(属性名) = **.**.** 转换器类名

2、全局转换器 名字开头必须是xwork

**.**.** (类名) = **.**.** 转换器类名



七、文件上传


、八、自定义拦截器

实现Interceptor接口  重写intercept方法

<package name="test" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="my" class="com.bjsxt.interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="test" class="com.bjsxt.action.TestAction">
<result>/test.jsp</result>
<interceptor-ref name="my"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>

九、OGNL表达式

OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言。是一种可以方便地操作对象属性的开源表达式语言。

1、“#”符号有三种用途: 

(1)、访问非根对象(struts中值栈为根对象)如OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性:   

 #request.userName相当于request.getAttribute("userName")   
(2)、用于过滤和投影(projecting)集合,如: books.{?#this.price>35}   

(3)、构造Map,如: #{'foo1':'bar1', 'foo2':'bar2'}  

#{'foo1':'bar1', 'foo2':'bar2'}这种方式常用在给radio或select、checkbox等标签赋值上。

2、“%”符号的用途是在标签的属性值被理解为字符串类型时,告诉执行环境%{}里的是OGNL表达式。

3、“$”有两种用途
 

(1)、在国际化资源文件中,引用OGNL表达式。

(2)、在Struts 2配置文件中,引用OGNL表达式:

<action name="saveUser" class="userAction" method="save"> 

 
  <result type="redirect">listUser.action?msg=${msg}</result>
 

</action>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: