Struts2 简单开发配置

长时间的做项目,使自己在开发的道路上并没有走远,很多知识都是懂得一些皮毛,没有知识的沉淀。现借此平台,好好把自己以前做过的东西整理以下,从一个更高的层次来学习以下,如果有错,还请大家能够不吝赐教。
Struts 2框架由WebWork和Struts1框架发展而来,struts2现在已经是一个成熟稳定的web mvc框架,接下来我将介绍如何去开发Strus2的"Hello World"。
1.首先我们需要在web.xml文件中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Struts Demo</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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>

</web-app>

在web.xml文件中配置了两个过滤器,一个是编码过滤器,一个就是struts2的请求过滤器。
2.其次,就是我们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>
<!-- 配置是否允许动态方法调用,例如http:///.../a!add.action -->
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
    <!-- 配置开发模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- struts2也可以在这里进行配置编码 -->
    <!-- <constant name="struts.i18n.encoding" value="gbk"></constant> -->

    <package name="login" namespace="/" extends="struts-default">
        <!-- 配置默认的action,前提是index必须得有 -->
        <!--<default-action-ref name="index" />
        <!-- 配置全局结果集 -->
        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>
        <!-- 声明式异常 -->
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings> 
        <!-- 配置一个action -->
        <action name="index" class="com.example.test.IndexAction">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

看上面的注释文件应该可以知道各个部门是什么意思了,可能还不太全,之后会再慢慢补全。
定义一个action类在struts2中非常方便,一个普通的类就可以做为一个action类,还可以通过实现action接口,最后就是继承一个ActionSupport类,此类已经给我们做好了一些封装属性和方法,如果没有特殊的要求,一般都是继承此类去开发action类。
这就是一个最简单的struts2开发的配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值