`

struts2注解笔记1

 
阅读更多
一  struts 2.1中,用convention去代替了code-behind,
 
convention用法:
1、默认拦截action,actions,struts,struts2包名下面的Action为结尾或者继承ActionSupport的类。

   

   当然也可以改 <constant name="struts.convention.package.locators" value="web"/> VALUE中指定
WEB目录及子目录下Action为结尾或者继承ActionSupport的类
,这样的话,不用再写struts.xml中的配置
   比如在地址栏中输入hello.action
则去找com.xxxx.web包下HelloAction.java的对应的action类中的excute方法,
如果是hello!hello.action,则找 HelloAction.java中的hello方法

然后去   WEB-INF\content\hello.jsp 找对应的页面显示。

如果不想设定这个目录,则可以设置:
<constant name="struts.convention.result.path" value="/WEB-INF/jsp/"/>

2 如果访问的名字为 hello-world.action

则访问HelloWorld.action类(去掉-号)  WEB-INF\content\hello-world.jsp

<constant name="struts.convention.action.name.separator" value="."/> 则可以改变多个单词之间的分隔符,

这样的话,可以用
    hello.world.action去访问,同时web-inf\content\hello.world.jsp要改成相应的.号


3 @action的使用
   @Action("/langsin")
public String mth1(){

System.out.println(" mth1 ok");

return SUCCESS;
}

    同时在web-inf/jsP中配置langsin.jsp,则访问xxxx/langsin.action,会访问mth1方法,输出mth1 ok
   如果有多个方法,多个action的注解,比如:
   @Action("/langsin")
public String mth1(){

System.out.println(" mth1 ok");

return SUCCESS;
}

@Action("/iceman")
public String mth2(){
System.out.println(" mth2 ok");

return SUCCESS;
}

  则可以xxxx/langsin!mth2.action访问mth2方法,输出System.out.println(" mth2 ok"),注意用的JSP
页为langsin.jsp,而不是用iceman.jsp
同理 xxx/iceman!mth1.action访问mth1方法,输出mth1 ok


4 如果src\com.web一个包,而在com.web下又有一个子目录user,比如src\com.web.user\UserAction.java,
则必须同时在web-inf\jsp\user\user.jsp,
然后访问时,用http://xxxx/user/user.action访问
 
  命名空间:

@Action("/langsin/iceman")
public String mth3(){

System.out.println(" mth3 ok");

return SUCCESS;
}
   则web-inf\jsp\langsin\iceman.jsp

5
  @Actions({
@Action("/one"),
@Action("/two")
})
public String mth4(){

System.out.println(" mth4 ok");

return SUCCESS;
}
  能同时映射多个,
   
6 返回值的映射
   @Action("rs")
public String mth5(){
System.out.println(" mth5 ok");

return "error";
}
    访问的是rs_error.jsp(action的名字-返回值的名字)


7 命名空间
   @Namespace("/helloworld")
public class HelloWorld extends ActionSupport {

  。。。。。
@Action("langsin")
public String mth1(){

System.out.println(" mth1 ok");

return SUCCESS;
}
     要用http://xxx/helloworld/langsin.action访问,并且映射的是web-inf\jsp\helloworld目录下的langsin.jsp
而如果  @Action("/langsin"),则覆盖掉namespace了,不需要用namespace
直接http://xxx/langsin.action即可,但访问的是web-inf\jsp下的langsin.jsp

8 拦截器
   @InterceptorRefs({
@InterceptorRef("otherInterceptor"),
@InterceptorRef("myInterceptorStack")
})
。。。。。。。。。。。。。。

9  <constant name="struts.convention.action.name.lowercase" value="false"/>
  说明JSP用大写映射规则,比如Hello-World.action,映射到Hello-World.jsp ,区分大小写,一般该值取TRUE
分享到:
评论

相关推荐

    struts2笔记

    很详细的struts2框架的添加,配置文件的编写,注解方式,HTTP解耦合,通配符,动态结果读取,有详细的案例。

    springmvc第一天课堂笔记

    springmvc框架原理(掌握) 前端控制器、处理器映射器、处理器适配器、视图解析器 springmvc入门程序 目的:对前端控制器、处理器映射器、处理器适配器、视图解析器学习 ...springmvc和struts2的区别

    SSH2--spring 2.5.6 + struts 2.2.1 + hibernate 3.5.5全部的注解的 M V C

    spring 2.5.6 + struts 2.2.1 + hibernate 3.5.5 内有 AOP 的切面、全部的注解的 M V C 结构,之外还在上网上找了一个同学的学习笔记一起打包,感觉好不错!

    SpringMVC笔记.zip

    使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring的 Spring MVC 框架或集成其他 MVC 开发框架,如 Struts1(现在一般不用),Struts2 等。SpringMVC 已经成为目前最主流的 MVC...

    吴天雄--SpringMVC笔记.doc

    SpringMVC笔记内容:第一天(SpringMVC的概述、SpringMVC与Struts2优势分析、重要组件、运行原理、SpringMVC环境搭建、@RequestMapping注解详解、字符编码过滤器、传参形式(基本数据类型或String、对象类型、对象....

    Spring的学习笔记

    三、 第三步:再来整合Struts2 41 四、 struts的读常量: 43 第十二课:DTO、VO 43 一、 DTO 43 二、 VO 43 第十二课:SSH整合存在的问题 43 一、 Jsp中访问Session时,Session已经关闭 43 二、 如果不配置事务,...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    spring课堂笔记的相关知识

    方便解耦,简化开发 Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring...Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持

    SpringMVC学习笔记整合搭建框架

    6、SpringMVC和Struts2的区别 第二天 1、高级参数绑定 a)数组类型的参数绑定 b)List类型的绑定 2、@RequestMapping注解的使用 3、Controller方法返回值 4、SpringMVC中异常处理 5、图片上传处理 6、Json数据交互 7...

    spring2.5 学习笔记

    三、 第三步:再来整合Struts2 41 四、 struts的读常量: 43 第十二课:DTO、VO 43 一、 DTO 43 二、 VO 43 第十二课:SSH整合存在的问题 43 一、 Jsp中访问Session时,Session已经关闭 43 二、 如果不配置事务,...

    spring学习笔记

    Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................

    javaee笔试题-src:源文件

    struts2最好别用最新的版本,否则有可能出现XXClassNotFound的情况,下载的压缩包里面有可以参考的xml文件 eclipse跑程序的时候要注意有可能因为页面缓存导致不显示新页面 maven: struts.xml等文件的路径可以参考...

Global site tag (gtag.js) - Google Analytics