Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放源代码 好了废话少说,进入正题,截止写稿时间struts最新版本是2.3,不过是12月12日发布的(2011年),为了稳定起见,我使用的版本是struts-2.2.3.1本文分为以下几个部分: 一、搭建开发和运行环境 1、到Apache下载Strut包:http://archive.apache.org/dist/struts/ 在这里可以下载到所有版本的struts,点击 ,之后如何操作就不讲了。下载之后解压。这个版本的struts的jar有很多,我们只需添加我们自己需要的就行了,我们先搭建起struts运行最基本的环境,当然jar包就是struts运行时所需的最少的jar包。所需jar如下: 网上有说需要这个又有说不需要这个,需要根据实际情况来定,实在不行就自己一个一个加,先从struts的core包开始,如果缺少jar包启动的时候就会报错,缺哪个加哪个。 2、打开MyEclipse(我用的是6.5)新建Web工程 新建项目名称为struts,选中JAVAEE5.0(这个不是必须的,但是现在都是这个,选一下也好),点击完成。 将准备好的jar包拷贝到lib目录下。 打开web.xml将其修改为如下代码: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app> 然后在src目录下新建一个struts.xml文件,内容如下: <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> </struts> 然后在新建一个内容为空的struts.properties文件。 至此项目搭建完毕,然后将其发布。 3、发布项目 在myeclipse里面可以嵌入tomcat,配置外部tomcat然后将项目发布,并运行。访问http://127.0.0.1:8080/struts/,弹出如下界面: 说明运行成功。 二、第一个Struts 2.0应用程序——Hello World 下面我们进行臭名昭著的Hello World程序。几乎所有语言的第一个程序都是Hello World。在src目录上右键,新建一个类,参数如下: 类的内容如下: package com.top.web; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; public String execute(){ return SUCCESS; } } 修改struts.xml内容如下: <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="" namespace="/" extends="struts-default"> <action name="helloWorld" class="com.top.web.HelloWorldAction"> <result>welcome.jsp</result> </action> </package> </struts> 在webroot目录下新建JSP页面,名字:welcome.jsp,内容如下: <%@ page language="java" pageEncoding="GBK"%> <html> <head> <title>Hello World!</title> </head> <body> Hello World! </body> </html> 目前位置目录结构应该是如下的: 重新发布运行,访问http://127.0.0.1:8080/struts/helloWorld.action出现如下界面,说明项目搭建成功:














- 粉丝: 6
- 资源: 11

我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
