struts2-零配置

来源:互联网 发布:天下数据员工 编辑:程序博客网 时间:2024/06/05 14:11

开发环境JDK1.8 eclipse struts2-2.3.31
1.创建web项目
2.导入struts2核心jar包,还需要额外导入一个包struts2-convention-plugin-2.xx.jar,只有导入了这个才能进行Annotation配置
3.更改web.xml文件(只配置好struts2的Filter就好)
4.创建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>    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <constant name="struts.devMode" value="true" /><!-- struts2 调试模式 -->    <!-- 进行扫描的根包,该包会被扫描成action -->    <constant name="struts.convention.action.packages" value="com.ifan.action" />    <!-- 指定的包会被进行扫描 -->    <constant name="struts.convention.package.locators" value="action,actions,struts,struts2" /><!-- 不进行扫描的包,用,分割,被包含的包,将不会被扫描成为action -->    <constant name="struts.convention.exclude.packages"        value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*" />    <!-- 默认扫描以Action结尾的类 -->    <constant name="struts.convention.action.suffix" value="Action" />    <!-- 结果result页面存放位置 -->    <constant name="struts.convention.result.path" value="/WEB-INF/content/" />    <!-- Action类文件重新自动加载 -->    <constant name="struts.convention.classes.reload" value="false" />     <!--用于生成action名字时的分隔符,比如DemoCustAction会被映射成为demo_cust,同时用于形成返回文件,比如访问DemoAction,return的值为input,插件会去指定不睦下,查找demo_input.jsp文件 -->     <constant name="struts.convention.action.name.separator" value="_" />     <!-- 开启动态调用函数,这个方法在struts2里面不推荐,可是在这里却可以实现动态方法的调用,不用自写action了 -->     <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <package name="convention-default" extends="struts-default">    </package></struts>

5.创建src/com.ifan.action.helloAction.java
这个包名的最后暂时最好是action,因为struts2有一个自己的对应规则,这个规则需要下一次详说,现在只进行简单的配置和演示,还有就是类名应该为 *Action.java样式,因为struts2默认扫描Action结尾的类。

package com.ifan.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.opensymphony.xwork2.ActionSupport;//设置返回值对应的页面//使用下面的Action可以访问到那个方法@Results({         @Result(name = "success", location = "hello.jsp"),         @Result(name = "fan", location = "hello_fan.jsp"),        @Result(name = "index", location = "hello/hello_index.jsp")        }) @Namespace("/aaa")public class helloAction extends ActionSupport {    // aaa/login  //hello/login  //login 都可以访问到该方法    //下面同理    @Action("/login")    public String execute() throws Exception {        System.out.println("-------------execute-----------------");        return SUCCESS;    }    @Action("/fan")    public String fan() {        // TODO Auto-generated method stub        System.out.println("------------fan------------------");        return "fan";    }    @Action("index")    public String index() {        // TODO Auto-generated method stub        System.out.println("------------index------------------");        return "index";    }}

6.创建WebContent/index.jsp 、WebContent/WEB-INF/content包、WebContent/WEB-INF/content/hello.jsp、WebContent/WEB-INF/content/hello_fan.jsp、WebContent/WEB-INF/content/hello/hello_index.jsp、WebContent/WEB-INF/content/hello/hello.jsp、WebContent/WEB-INF/content/hello/login.jsp,都创建完成之后可以对每个页面进行一个标记,然后在浏览器输入各种action进行页面的跳转和方法的调用。
例如:
项目名/struts2_009/hello.action
项目名/struts2_009/fan.action
项目名/struts2_009/aaa/login.action等
可以自己调试。

3 0
原创粉丝点击