本文简单描述如何在Eclipse中使用使用Struts2,并介绍一下Struts2的配置文件

注:Struts2默认需要Java 5.0及其以上版本的运行环境支持,Web容器需要支持Servlet 2.4和JSP 2.0

一、Eclipse+Struts

1.新建项目

在Eclipse中新建Dynamic Web Project,项目名为StrutsPro,在WEB-INF/lib目录下添加Struts 2框架的jar包,项目结构如下图所示:

2.配置web.xml

编辑项目中的web.xml文件,在该文件中配置Struts 2的核心Filter,编辑后的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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StrutsPro</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

解说:

代码第6行定义了核心过滤器的名称为struts2

代码第7行配置核心Filter的实现类为org.apache.struts2.dispatcher.FilterDispatcher

代码第11行用来配置核心过滤器过滤所有的Web请求

配置完web.xml文件后,项目就已经添加好Struts 2框架了

3.配置struts.xml

struts.xml是Struts 2框架的核心配置文件,主要负责管理Struts 2框架下的业务控制器Action

struts.xml放置在项目的WEB-INF/classes路径下,需要在struts.xml文件中添加XML规范、DTD以及根目录信息,编辑后的struts.xml文件如下:

 <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 指定Struts 2配置文件的DTD信息 -->
<struts><!-- 根节点 -->
</struts>

解说:

代码第1行为XML声明

代码第2行至第4行用来指定Struts 2配置文件的DTD信息

XML的根节点为struts,所有的配置信息都放置在该节点之下。

二、Maven+Struts

暂略

三、配置文件

3.1 struts-default.xml文件

struts-default.xml文件包含在Struts2-core-2.x.x.jar中,是struts2框架默认加载的配置文件,它定义了一些bean和拦截器,为框架提供默认设置

此外,Struts2有两个核心配置文件:struts.xml文件、struts.properties文件

3.2 struts.xml文件

struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等,访问路径 = 命名空间 + 动作名称

include  引入其它配置文件
package

属性:name

  包的名称,保持唯一。

属性:extends

  一般情况下需要继承struts-default包,但不是必须的,如果不继承将无法使用struts2提供的核心功能

  struts-default在struts-default.xml中定义

属性:namespace

  命名空间

action

属性:name

  动作名称

属性:class

  类的路径

属性:method

  方法名称

result  
global  
constant

设置请求URL的扩展名:<constant name="struts.action.extension" value="do"></constant>

设置开发者模式:<constant name="struts.devMode" value="true"></constant>

   
   
   

案例:

<?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>
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" /> <include file="new-define.xml"></include> <package name="studentMgr" extends="struts-default" namespace="csu">
<interceptors>
<interceptor name="filter" class="com.csu.filter.CommitFilter"></interceptor> <interceptor-stack name="mystack">
<interceptor-ref name="filter"></interceptor-ref>
</interceptor-stack>
</interceptors> <action name="register" class="com.csu.action.LoginAction"> <interceptor-ref name="mystack"></interceptor-ref> <result name="success" type="dispatcher">/studenInfo.jsp</result>
<!--
参数设置
name:对应Action中的get/set方法
-->
<param name="url">http://www.csu.com</param> </action>
</package> </struts>

3.3 struts.properties文件

struts.properties文件通常放在Web应用的WEB-INF/classes路径下,是一个标准的Properties文件,该文件定义了Struts 2框架的大量属性,每个key就是一个Struts 2属性,该key对应的value就是一个Struts 2属性值,开发者可以通过改变这些key-value来满足应用的需求。

属性 含义
struts.configuration 该属性指定Struts2的配置文件管理器,该属性的默认值是org.apache.Struts2.config.DefaultConfiguration;开发者可以自行实现Configuration接口来加载Struts2的配置文件
   
   
   
   
   

参考资料:

Eclipse怎样配置struts2_百度经验

http://jingyan.baidu.com/article/fd8044fafdf0a25030137a7c.html

Struts 2(二):使用Struts2的更多相关文章

  1. struts 2读书笔记-----struts2的开发流程

    一.将struts 2的lib文件夹下地commons-fileupload.jar.commons-io.jar.freemarker.jar.javassist.jar.ognl.jar.stru ...

  2. (二)Struts2 核心知识

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 get/set 自动获取/设置数据 action代码: ...

  3. struts2系列(二):struts2参数传递错误、struts2的输入错误验证

    一.struts2参数传递错误 1. 基本数据类型的传递最好使用包装类,原因是struts 2.1之后使用基本数据类型如果参数为空会报错2. 日期参数的传递最好定义一个区域的属性(定义locale), ...

  4. Struts笔记二:栈值的内存区域及标签和拦截器

    值栈和ognl表达式 1.只要是一个MVC框架,必须解决数据的存和取的问题 2.struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 1.  ValueStack是一个接口,在struts ...

  5. (二)Struts2配置文件

    一.web.xml文件 web.xml配置文件是一种J2EE配置文件,决定servlet容器的HTTP元素需求如何进行处理.它严格来说不是一个Struts2 配置文件,但它是Struts2 运作所需要 ...

  6. java的三大框架(二)---Struts2

    Strtu2框架 1.控制器:ActionServlet充当控制层 2.模型层:由ActionForm及业务JavaBean实现 3.视图:用户的看到并与之交互的界面   由struts标签库和jsp ...

  7. struts征程:1.初识struts2

    1.struts2在开发中所必须用到的jar包导入到项目的lib目录下 2.在web.xml中配置一个过滤器,代码格式如下 <filter> <filter-name>stru ...

  8. Struts(十二):异常处理:exception-mapping元素

    配置当前action的声明异常处理 1.exception-mapping元素中有2个属性 exception:指定需要捕获的异常类型 result:指定一个响应结果,该结果将在捕获到异常时被执行.即 ...

  9. 菜鸟学SSH(二)——Struts2国际化手动切换版

    国际化(internationalization,i18n)和本地化(localization,l10n)指让产品(出版物,软件,硬件等)能够适应非本地环境,特别是其他的语言和文化.程序在不修改内部代 ...

  10. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(二)——Struts2集成

    1. pom.xml文件添struts2依赖jar包: <!-- 与Struts2集成必须使用 --> <dependency> <groupId>org.spri ...

随机推荐

  1. Unicode字符集和UTF-8, UTF-16, UTF-32编码

    ASCII 在过去的计算中,ASCII码被用来表示字符.英语只有26个字母和其他一些特殊字符和符号. 下表提供了ASCII字符及其相应的十进制和十六进制值. 可以从上面的表中推断,在十进制数系统中,A ...

  2. 关于ie8下监听input事件的不兼容问题。

    关于在ie8下,监听输入框的值变化的input事件不支持的解决办法: 很懒...直接上原文地址.... 原文地址:http://www.cnblogs.com/lhb25/archive/2012/1 ...

  3. SSM框架优缺点和spring boot 比起优缺点是什么?

    一.SSM优缺点应该分开来说的,比如 1)spring 不说了,核心ioc.aop技术,ioc解耦,使得代码复用,可维护性大幅度提升,aop提供切面编程,同样的增强了生产力. 2)spring mvc ...

  4. 跳转到系统设置界面 iOS

    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApp ...

  5. openssl生成iis需要的pfx格式的证书

    合成.pfx证书 将私钥文件(server.key)和服务器crt证书文件(server.crt ),放到openssl安装目录的bin目录下. 控制台也进到此目录下,然后执行下面指令. openss ...

  6. Bluetooth® Low Energy Beacons

    Bluetooth® Low Energy Beacons ABSTRACT (abstract ) 1.This application report presents the concept of ...

  7. SSIS中出现数据流数据源假死状态的解决办法

    相信开发过Sql Server SSIS的人都遇到过在数据流中数据源假死的问题,特别是Excel Source特别容易假死,当job执行到数据流中的Excel Source时,既不报错也不执行,也没有 ...

  8. Lua库-string库

    string.len(s) string.rep(s,n) string.lower(s) string.upper(s) string.sub(s,i);//截取s第i个开始的后缀 string.s ...

  9. php生成word文档

    使用fopen文件操作函数来做,需要注意的直接生成中文文件名会乱码,(生成word和微软的编码不一样)需要转码生成.word内容保持utf8编码就好. $file_name = iconv(" ...

  10. 修改系统UITableViewCell的ImageView大小

    代码如下: CGSize itemSize = CGSizeMake(, ); UIGraphicsBeginImageContext(itemSize); CGRect imageRect = CG ...