Structs2开发环境 的配置

1.导入依赖的jar包


本博文配置Structs2的前提是完成 jdk、tomcat、myeclipse以及相关数据库的配置,能够用jsp+servlet完成java web服务程序的编写

http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x-all.zip,目前最新版为2.2.3。下载完后解压文件,开发struts2应用需要依赖的jar文件在解压目录的lib文件夹下。不同的应用需要的JAR包是不同的。下面给出了开发Struts 2程序最少需要的JAR。

  • struts2-core-2.2.3.jar :Struts2框架的核心类库 xwork-core-2.2.3.jar
  • XWork类库,Struts 2在其上构建
  • ognl-3.0.1.jar :对象图导航语言(Object Graph Navigation Language),Struts 2框架使用的一种表达式语言cons-io-2.0.1.jar
  • freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
  • commons-logging-1.1.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK
    1.4+的日志记录。

    如果 记不住需要导入的包名,打开一个空的项目struts2-blank.war看看里面所引的包即为开发所需的最基本的包
    还有一种最省事的做法是:如果不需要跟第三方框架集成,把不带-plugin结尾的jar文件都添加入类路径。如果需要跟第三方框架集成,这时候还需要加入对应的-plugin jar文件。例如跟spring集成,还需要再加入struts2-spring-plugin-2.x.x.jar.
    下图为我新建一个 最简单的工程所引入的jar包
    引入jar包的操作:选中 WEB-INF目录下的llib,右击–>Build Path–>Configure Bulid Path
    需要引入的jar包

2.Struts2的启动配置(web.xml)


2.1在web.xml中加入struts2支持

其实就是新建了一个过滤器,让程序指向structs2

  <!-- 加入了struts2支持 -->
    <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.定义Action类


简单的action类示例

package com.demo.action;

import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.tiedao.pojo.TbUser;
import freemarker.template.utility.Execute;


public class HelloWorldAction{
    public String execute() throws Exception {
        System.out.println("执行了 ------------------String execute()");
        return "success";
    }           
}

4.编写结果页面

简单的index.jsp和 success.jsp示例
index.jsp的body标签部分

  <body>
      <a href="helloWorld.action">访问HelloWorldAction中的execute()</a>
  </body>
  • success.jsp略*

5.Struts2的配置文件

在src目录下新建一个structs2的配置文件 structs.xml

一个简单的structs.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>
    <package name="###" extends="struts-default">
        <!-- name="helloWorld" 是对应JSP界面中的URL请求地址 
             class="com.demo.action.HelloWorldAction" 跳转到action
        -->
        <action name="helloWorld" class="com.demo.action.HelloWorldAction">
            <result name="success">/WEB-INF/success.jsp</result>
        </action>

    </package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值