菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
81
0

Struts2 - Convertion

原创
05/13 14:22
阅读数 44891

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,这与普通的使用没区别。

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!-- 把它设置为开发模式,发布时要设置为false -->
    <constant name="struts.devMode" value="true" />
    <!-- 设置在class被修改时是否热加载,发布时要设置为false -->
    <constant name="struts.convention.classes.reload" value="true"/>
    <!-- 自动动态方法的调用,使用这个设置后可以这样调用:action!method -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 指定jsp文件所在的目录地址 -->
    <constant name="struts.convention.result.path" value="/WEB-INF/content/" />
    <!-- 使用struts-default默认的转换器,如果是rest的使用:rest-default,rest需要rest的jar插件 -->
    <constant name="struts.convention.default.parent.package" value="struts-default"/>
    
</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

package com.my.actions;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
    
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String test() {
        System.out.println("test");
        return "test";
    }
    
    public String edit(){
        return "edit";
    }

}

 

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

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    edit page.<br/>
    user: ${ requestScope.username }
</body>
</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标注:

    @Action("login-show")
    public String show(){
        return "show";
    }

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

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

 

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

 

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

发表评论

0/200
81 点赞
0 评论
收藏