Struts2应用开发详解-一、开发环境的搭建

Struts2应用开发详解--1、开发环境的搭建

说明:本系列文章适合于有J2EE开发经验程序员快速学习使用Struts2。

 

一、文件准备:

从如下地址http://struts.apache.org/download.cgi 下载相关文件,可以选*lib.zip或者*all.zip。

下载完毕后再解压的lib目录中挑选相关的jar文件。我用的是struts-2.2.1 所以应用中需要的文件为commons-fileupload-1.2.1.jar、commons-logging-1.0.4.jar、freemarker-2.3.16.jar、ognl-3.0.jar、struts2-core-2.2.1.jar、xwork-core-2.2.1.jar、commons-io-1.3.2.jar、struts2-2.2.1-all/app/struts2-blank-2.2.1.war/WEB-INF/lib/javassist-3.7.ga.jar。

 

二、新建项目,创建一个Web Project即可。

项目创建后将.jar文件引入lib中。

 

三、编写Struts2的配置文件:

1、struts.xml文件

新建struts.xml文件于src目录下。

该文件模板内容如下:

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

</struts>

也可以选择从下载文件的例子中拷贝相关内容。

2、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>***</display-name>

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

以上文件修改完成后,开发框架即完成搭建,可以正常启动web服务。如果启动失败请检查是否因为Struts的新版本更新添加的jar问件没有引入。引入后即正常。