struts2: config-browser-plugin 与 convention-plugin 学习

struts2被不少新手诟病的一个地方在于“配置过于复杂”,相信很多初学者由于这个直接改投Spring-MVC了。convention-plugin、 config-browser-plugin这二个插件的出现,很大程度改善了这个囧境。html

简言之:convention-plugin采用"约定大于配置”的思想,只要咱们遵照约定,彻底能够少写配置甚至不写配置;而config-browser-plugin则用于方便的浏览项目中的全部action及其与jsp view的映射。这二个插件结合起来学习,能很方便的搞定struts2中各类复杂的action-view映射需求。java

1、config-browser-plugin使用apache

<span style="font-size:14px;">
 <dependency>
     <groupId>org.apache.struts</groupId>
     <artifactId>struts2-config-browser-plugin</artifactId>
     <version>2.3.16</version>
 </dependency>

</span>

maven项目的pom.xml中加上这个便可,运行后,浏览 http://localhost:8080/{你的项目名称}/config-browser/ 便可看到当前项目中的全部action 服务器

注:如下内容中,凡是有url的地方,项目名称假设为struts2-helloworldapp

若是跑不起来,检查服务器应用WEB-INF/lib/下是否有struts2-config-browser-plugin-2.3.16.jar 这个文件jsp

2、convention-plugin 使用maven

<span style="font-size:14px;"><dependency>
     <groupId>org.apache.struts</groupId>
     <artifactId>struts2-convention-plugin</artifactId>
     <version>2.3.16</version>
 </dependency></span>

pom.xml中加上这个后,能够把struts.xml配置文件给干掉了(或者改个名),部署App,若是启动正常,则表示环境ok。若是提示缺乏asm 啥类,检查下面这几个依赖项,是否也加进来了

<span style="font-size:14px;"><dependency>
    <groupId>asm</groupId>
    <artifactId>asm</artifactId>
    <version>3.3.1</version>
</dependency>

<dependency>
    <groupId>asm</groupId>
    <artifactId>asm</artifactId>
    <version>3.3.1</version>
</dependency>

<dependency>
    <groupId>asm</groupId>
    <artifactId>asm-commons</artifactId>
    <version>3.3.1</version>
</dependency>

<dependency>
    <groupId>asm</groupId>
    <artifactId>asm-tree</artifactId>
    <version>3.3.1</version>
</dependency></span>

2.1 零action的view学习

convention-plugin约定全部的jsp view都放在WEB-INF/content目录下,在这个目录下先随便放一个名为"no-action.jsp"的jsp文件,里面随便写点啥this

浏览 http://localhost:8080/struts2-helloworld/no-actionurl

即:即便没有对应的Action类,struts2也能按约定正常展示页面。(固然,这只是开胃小菜,真正应用中,除了作一些纯静态的页面原型以外,大部分场景,背后仍是要有Action类来支撑的)

2.2 常规映射

建一个HelloWorld.action类

<span style="font-size:14px;">package com.cnblogs.yjmyzz.action;

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

@Namespace("/home")
public class HelloWorldAction extends BaseAction {

    private static final long serialVersionUID = -8827776224243873974L;

    private String message;

    @Action("hello-world")
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Action(value = "say-hi", results = { @Result(name = "success", location = "hello-world.jsp") })
    public String sayHi() throws Exception {
        message = "welcome to SSH!";
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}</span>

解释一下:

第7行,在整个Action类上使用了@Namespace("/home"),表示整个Action最终浏览的url,是以 http://localhost:8080/{你的项目名称}/home/ 打头

第14行,经过注解@Action("hello-world"),把默认的/home/index.action路径,改为了 /home/hello-world

至于execute方法,返回success后,对应的是哪一个jsp文件,这个不用死记,经过config-browser-plugin看下便知

即:execute方法返回input/error/success中的任何一个,都会映射到/WEB-INF/content/home/hello-world.jsp 这个文件上

20行sayHI()方法上的注解有点意思,@Action(value = "say-hi", results = { @Result(name = "success", location = "hello-world.jsp") }),默认状况下,若是不指定location,返回success时,它应该对应 /WEB-INF/content/home/say-hi.jsp这个文件,可是经过location值,变成了hello-world.jsp,即与/home/hello-world共用同一个view.

 

三、拦截器问题

上一篇学习了经过拦截器来处理异常,采用convention插件后,会发现拦截器不起做用(struts.xml中配置了也同样)

<span style="font-size:14px;"><?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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">

        <interceptors>
            <interceptor name="myinterceptor"
                class="com.cnblogs.yjmyzz.Interceptor.ExceptionInterceptor">
            </interceptor>

            <interceptor-stack name="myStack">
                <interceptor-ref name="myinterceptor" />
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="myStack" />
        <default-action-ref name="index" />

        <global-results>
            <result name="error">/WEB-INF/common/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="error" />
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">hello-world</param>
                <param name="namespace">/home</param>
            </result>
        </action>

    </package>

<!-- 由于有Convention-plugin,就再也不须要手动写action-view的映射规则了 -->
<!--     <include file="struts-home.xml" />
    <include file="struts-mytatis.xml" />  -->

</struts></span>

缘由在于convention-plugin使用后,全部Action再也不继承自默认defaultStack对应的package,为了解决这个问题,建议全部Action都继承自一个自定义的BaseAction类,而后在BaseAction上加上@ParentPackage("default"),让全部Action强制继承自struts.xml中定义的default package

<span style="font-size:14px;">package com.cnblogs.yjmyzz.action;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("default")
public class BaseAction extends ActionSupport {

    private static final long serialVersionUID = -4320398837758540242L;
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

}</span>

基本用法就是这些,若是不清楚Action最终出来的url是啥,或者不清楚某个url最终会对应到哪一个jsp文件,无需死记规则,只要善用config-browser-plugin,大多数下不用查阅文档便可快速解决问题。

做者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。


另外说一下我遇到的问题:缺乏asm三个架包所致使(asm-3.3.jar,asm-commons-3.3.jar,asm-tree-3.3.jar)一直404错误,还有就是我犯了一个严重的错误:没看错误提示,致使我在哪里啥弄了一天,痛苦。。。。

相关文章
相关标签/搜索