首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Struts2环境筹建

2012-08-26 
Struts2环境搭建一.搭建struts2应用需要使用到的最基本的JAR包%STRUTS2_HOME%\lib\commons-fileupload-1.2

Struts2环境搭建

一.搭建struts2应用需要使用到的最基本的JAR包

%STRUTS2_HOME%\lib\commons-fileupload-1.2.1.jar??? 文件上传组件,2.16版本后必须加入此文件

%STRUTS2_HOME%\lib\commons-io-1.3.2.jar??? 日志包

%STRUTS2_HOME%\lib\freemarker-2.3.16.jar??? struts2的UI标签的模板使用FreeMarker编写

%STRUTS2_HOME%\lib\javassist-3.7.ga.jar

%STRUTS2_HOME%\lib\ognl-3.0.jar??? 对象图导航语言,通过OGNL读写对象属性

%STRUTS2_HOME%\lib\struts2-core-2.2.1.1.jar??? struts2核心类库

%STRUTS2_HOME%\lib\xwork-core-2.2.1.1.jar??? XWork类库,struts2在其上构建

?

二.在类路径下加入Strus2配置文件:struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 指定请求后缀 --><constant name="struts.action.extension" value="do,action" /><!-- 指定默认编码集,作用域HttpServletRequest的setCharacterEncoding方法 --><constant name="struts.i18n.encoding" value="UTF-8" /><!-- 设置浏览器是否缓存静态内容,默认为true(生产环境下使用) --><constant name="struts.serve.static.browserCache" value="false" /><!-- 系统是否自动重新加载该文件,默认为false(生产环境下使用) --><constant name="struts.configuration.xml.reload" value="true" /><!-- 开发模式 --><constant name="struts.devMode" value="true" /><!-- 与Spring集成时,指定由Spring负责action对象的创建 <constant name="struts.objectFactory" value="spring" /> --><!-- 是否支持动态方法调用,默认值为true --><constant name="struts.enable.DynamicMethodInvocation" value="true" /><!-- 上传文件的大小限制 --><constant name="struts.multipart.maxSize" value="10701096" /><!-- sturt2中使用包(package)来管理action,name属性指定了包名,方便其他包继承 ,namespace属性指定了命名空间,其指定了action的访问路径extends属性指定了该包继承自哪个包,struts-default包定义了struts框架常用的一些拦截器,通常每个包都应继承struts-default包--><package name="hello" namespace="/test" extends="struts-default"><!-- 定义actionname属性指定了action的名称,同时也作为action访问路径的一部分class属性指定了action的实现类method方法指定了action处理请求的方法名称 --><action name="helloWorld" method="doAction"><!-- result属性指定了action的返回视图name属性指定了返回视图的名称 --><result name="sucess">/WEB-INF/page/hello.jsp</result></action></package></struts>
?

三.在web.xml中加入struts2 MVC框架启动配置

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

?四.开发HelloWorldAction

package com.chris.action;public class HelloWorldAction {/** * 处理请求的方法 * 方法名与配置文件中method属性一致 * 返回类型必须为String类型 */public String doAction() {return "sucess";}}

?

将应用部署到服务器上,这样就可以处理访问路径为/test/helloWorld.do的请求了

热点排行
Bad Request.