Struts2中的Convertion插件是比较有用,它可以简化很多操作,比如不需要一个一个Action在struts.xml中设置。当然,最有意义的是它与rest插件一起整合使用,可以完成web-site中的restful操作。具体可以参考Struts包中的例子:

http://struts.apache.org/release/2.3.x/docs/convention-plugin.html

http://struts.apache.org/development/2.x/docs/rest-plugin.html

rest plugin的使用后面再写,先试下convertion plugin。由于struts的配置太多,只例出一些比较实用和常用的先,其它的之后再整理一下。

eclipse建j2ee工程就不屁话了。

1)加入包:struts2-convention-plugin-2.3.16.1.jar

最好把另一个包也加上(发布时要把它删除,不然别人就可以很容易就得到所有的action list):struts2-config-browser-plugin-2.3.16.1.jar

browser这个plugin可以用于查看当前web-site所有的struts action和明细。使用方法:

在浏览器地址栏上输入:http://localhost:8080/{WEB-SITE}/config-browser/actionNames.action

比如:http://localhost:8080/TestWeb/config-browser/actionNames.action

2)修改web.xml,加入Struts2的filter,这与普通的使用没区别。

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  4. </filter>
  5.  
  6. <filter-mapping>
  7. <filter-name>struts2</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

3) 在src目录下新建一个struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <!-- 把它设置为开发模式,发布时要设置为false -->
  9. <constant name="struts.devMode" value="true" />
  10. <!-- 设置在class被修改时是否热加载,发布时要设置为false -->
  11. <constant name="struts.convention.classes.reload" value="true"/>
  12. <!-- 自动动态方法的调用,使用这个设置后可以这样调用:action!method -->
  13. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  14. <!-- 指定jsp文件所在的目录地址 -->
  15. <constant name="struts.convention.result.path" value="/WEB-INF/content/" />
  16. <!-- 使用struts-default默认的转换器,如果是rest的使用:rest-default,rest需要rest的jar插件 -->
  17. <constant name="struts.convention.default.parent.package" value="struts-default"/>
  18.  
  19. </struts>

这里最重要的的就这几个设置。(留意xml的注释)
需要说明一下的是这两个key

name=struts.enable.DynamicMethodInvocation,这个设置为true,就可以动态的调用action中的public方法,如果设置false,就不可以调用了。

name=struts.convention.result.path,这个设置的值是action对应的jsp文件所存放的目录所在地址。如果没有设置这个值,其实stutrs2默认就是/WEB-INF/content这个目录。

4)新建一个package:com.my.actions,及在这个package中新建一个LoginAction.java

  1. package com.my.actions;
  2.  
  3. import org.apache.struts2.convention.annotation.Action;
  4.  
  5. import com.opensymphony.xwork2.ActionSupport;
  6.  
  7. @SuppressWarnings("serial")
  8. public class LoginAction extends ActionSupport {
  9.  
  10. private String username;
  11.  
  12. public String getUsername() {
  13. return username;
  14. }
  15.  
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19.  
  20. @Override
  21. public String execute() throws Exception {
  22. return SUCCESS;
  23. }
  24.  
  25. public String test() {
  26. System.out.println("test");
  27. return "test";
  28. }
  29.  
  30. public String edit(){
  31. return "edit";
  32. }
  33.  
  34. }

5)在/WEB-INF/content目录下新建三个jsp:login.jsp、login-test.jsp、login-edit.jsp

login.jsp和login-test.jsp内容随便,login-edit.jsp的内容为:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. edit page.<br/>
  11. user: ${ requestScope.username }
  12. </body>
  13. </html>

主要用于打印username。

好了,完成了。

在浏览器中键入:

http://localhost:8080/TestWeb/login

-- 这个是会自动定位到login.jsp

http://localhost:8080/TestWeb/login!test

-- 这个会定位到test()方法

http://localhost:8080/TestWeb/login!edit?username=robin

-- 这个会定位到edit()方法,同时传入一个username参数并在页面中显示。


事实上,我们还可以在LoginAction.java中再加一个@Action标注:

  1. @Action("login-show")
  2. public String show(){
  3. return "show";
  4. }

这样就会有一个新的Action所产生,可以在浏览器中输入:http://localhost:8080/TestWeb/login-show

Action的明细可以在browser plugin中查看。

jsp的命名规则是<Action>-<Method>.jsp,如:login-test.jsp

很不喜欢方法的调用使用感叹号!还是使用rest吧哈。

Struts2 - Convertion的更多相关文章

  1. Struts2 - Rest(1)

    Struts2提供了一个restful的插件:struts2-rest-plugin-2.3.16.1.jar 这个插件可以把Struts2当做restful来使用,不过它的rest功能目前来说有点“ ...

  2. Struts2 注解零配置方法(convention插件使用)

    最近接触到一个新的项目,是做一个使用S2SH的电子商务商城的二次开发.之前使用过S2SH,在此之前的项目中,Struts2 使用的是XML配置而这个项目是使用注解.在这个项目中,注解还不需要使用Act ...

  3. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  4. 菜鸟学Struts2——Interceptors

    昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是 ...

  5. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  6. 菜鸟学Struts2——Results

    在对Struts2的Action学习之后,对Struts2的Result进行学习.主要对Struts2文档Guides中的Results分支进行学习,如下图: 1.Result Types(Resul ...

  7. 菜鸟学Struts2——Actions

    在对Struts2的工作原理学习之后,对Struts2的Action进行学习.主要对Struts2文档Guides中的Action分支进行学习,如下图: 1.Model Driven(模型驱动) St ...

  8. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  9. 13、零配置Struts2开发

    Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...

随机推荐

  1. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  2. 将文件从一台linux机器拷贝到多台的方法

    首先你所操作的各台linux机器间必须设置了ssh免密码登录,具体方法可上网查看.将文件从一台linux机器拷贝到多台分为以下几个步骤: 第一步:创建脚本文件remotecopy.sh #!/bin/ ...

  3. Linux ---pptpd部署

    PPTP 全称为 Point to Point Tunneling Protocol -- 点到点隧道协议,是VPN协议中的一种. 一.CentOS 6.2 下 PPTP VPN 服务器安装 1.安装 ...

  4. leetcode 138. Copy List with Random Pointer ----- java

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. Codeforces Round #119 (Div. 2)

    A. Cut Ribbon \(f(i)\)表示长为\(i\)的布条最多可以剪几段. B. Counting Rhombi \(O(wh)\)枚举中心计算 C. Permutations 将序列一映射 ...

  6. URAL 1208 Legendary Teams Contest(DFS)

    Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...

  7. 一些基本的C/C++数据类型

    size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...

  8. 越狱Season 1- Episode 16

    Season 1, Episode 16 -Burrows:Don't be. It's not your fault. 不要,不是你的错 -Fernando: Know what I like? 知 ...

  9. Fortran编译多个文件(转载)

    最近需要在Linux系统下编译多个Fortran程序,在网上搜索了一下,但是资料不多,也许因为这个问题比较简单,不值一提,但还是把我知道的写出来,供大家参考: 方法一: 假如现在有两个Fortran程 ...

  10. Syscall param open(filename) points to unaddressable byte(s)

    valgrind 调试出现如题所示的错误,原因是存取文件名的空间被释放了 源代码: cfg->snteam_cfg->snt.score.nd.wrd_dict_db_fn=cfg-> ...