1、在官网下载Struts2的开发包

下载链接如下:

http://120.203.229.30/5ff/2bc79/5ff16ae8698e1c321758a8f03a1bc0939892bc79/struts-2.3.16.3-all.zip?n=struts-2.3.16.3-all.zip

http://mirrors.cnnic.cn/apache//struts/documentation/struts-2.3.16.3-docs.zip

http://mirrors.cnnic.cn/apache//struts/source/struts-2.3.16.3-src.zip

Struts2当前最新版本为:

解压struts-2.3.16.3-all.zip得到struts-2.3.16.3,开发包的jar文件都存放在lib文件夹中。

2、加载jar包到工程中的库文件夹部分,使用时候最好都包好红框中的7个jar文件。

如何创建名称为struts2的用户库文件夹,方法如下:

eclipse窗口的菜单栏中选择window--Preferences,选择Java--Build Path,点击下面的User Libraries。然后新建即可。

3、设置工程属性,使得编译时候能够将用户引入的库文件自动拷贝到WEB-INF/lib路径下去编译工程,如果没有此步,会导致编译的时候找不到class的错误。这里当然也可以直接将库文件放到lib中去,但是建议使用前面的方法。

4、新建index.jsp文件,同时拷贝jsp文件必要的库文件servlet-api.jar文件到WEB-INF/lib目录下

代码解释:第12行的value必须等于"mes",因为这里的"mes"是和HelloWorldAction类中定义的私有变量private String mes保持一致的,与方法对应与HelloWorldAction类中的String getMes()方法。

 <%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome Struts2(欢迎页面)</title>
</head>
<body>
<h2>
<s:property value="mes"/>
<!-- value is equal to HelloWorldAction.java private mes -->
</h2>
</body>
</html>

5、Run index.jsp在Tomcat服务器上运行,运行后将在eclipse左侧的工程栏自动生成Servers目录。编辑目录下的web.xml文件,添加如下代码:

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

请注意编写时候,filter节点和filter-mapping节点下的filter-name必须保持一致,均为struts2,且filter-class务必书写准确。

注:该步骤运行index.jsp主要是要自动生成web.xml文件,然后按照要求设置xml文件,以供struts2框架配置

另外特别注意,Apache Tomcat的安装路径不能存在空格,否则会可能会出一些意想不到的错误。

6、编写Action类

Struts2的核心功能是Action类,Action类是一段特定的URL请求时执行的代码,过滤器(FilterDispatcher)会根据请求的URL不同,执行相应的Action类,Action类执行的结果一般对应于一个result展现给用户。result通过字符串名字来标识,过滤器根据Action返回的结果字符串选择对应的result展示给用户,Action与其对应的result在struts.xml文件中进行配置。

  一般Action类会继承com.opensymphony.xwork2.ActionSupport类,并重写此类中的execute()方法。在src目录下创建一个新类HelloWorldAction.java,代码如下:

 //javac -classpath "C:\Program Files\Java\jdk1.7.0_60\lib\tools.jar;D:\Java\struts-2.3.16.3-all\struts-2.3.16.3\lib\xwork-core-2.3.16.3.jar" HelloWorldAction.java

 package com.struts2;
import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{
private String mes; public String getMes(){
return mes;
}
public void setMes(String _mes){
this.mes=_mes;
} public String execute()throws Exception{
mes="Hello World!";
return SUCCESS;
}
}

手动编译生成class文件,编译命令为:
javac -classpath "C:\Program Files\Java\jdk1.7.0_60\lib\tools.jar;D:\Java\struts-2.3.16.3-all\struts-2.3.16.3\lib\xwork-core-2.3.16.3.jar" HelloWorldAction.java

7、配置struts.xml文件(...\WebContent\WEB-INF\classes\路径下)

 <?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>
<include file="stuts-default.xml"></include>
<package name="default" extends="struts-default">
<action name="Hello" class ="com.struts2.HelloWorldAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

第6行,action节点中的name属性等于"Hello",这里就对应访问action的URL名称:http://localhost:8080/StrutsDemoProject/Hello.action

com.struts2.HelloWorldAction 为HelloWorldAction的class路径

8、拷贝struts2开发包中必要的jar文件到Tocmat安装路径下的lib文件夹中,运行index.jsp程序,这时应该不会在eclipse的Console窗口出现class not found 的错误。(这里需要注意如果在WEB-INF/lib下和tomcat的安装目录的lib下面同时放了struts2的jar文件的时候,这个时候会出现jar文件冲突的错误,另外也可能出现其他意想不到的问题。届时,apache运行失败,问题很难排查。)

C:\apache-tomcat-8.0.9\lib下新增的struts2的jar文件列表如下:

struts2-core-2.3.16.3.jar   ognl-3.0.6.jar  freemarker-2.3.19.jar

commons-logging-1.1.3.jar  javassist-3.11.0.GA.jar

xwork-core-2.3.16.3.jar   commons-lang3-3.1.jar

正常情况下此时运行index.jsp的Console界面是:(这个的错误原因是reques是null,这里是属于正常现象。因为http://localhost:8080/StrutsDemoProject/Hello.action就会正常显示了。)

七月 27, 2014 10:53:38 下午 org.apache.struts2.dispatcher.Dispatcher error
严重: Exception occurred during processing request: null
java.lang.NullPointerException

9、struts2部署后的,运行界面如下

到这里就完成了sturts2的开发环境部署。

【J2EE】struts-2.3.16.3+apache-tomcat-8.0.9开发环境部署,“Hello World”的实现。的更多相关文章

  1. Target runtime Apache Tomcat v6.0 is not defined

    在工程目录下的.settings文件夹里,打开org.eclipse.wst.common.project.facet.core.xml文件,其内容是: <?xml version=" ...

  2. Target runtime Apache Tomcat v7.0 is not defined.

    打开项目,找到.settings--->org.eclipse.wst.common.project.facet.core 修改这个文件中: <?xml version="1.0 ...

  3. Java compiler level does not match the version of the installed Java project facet. springmvc1 和 Target runtime Apache Tomcat v7.0 is not defined.

    Java compiler level does not match the version of the installed Java project facet.springmvc1 : Targ ...

  4. Target runtime Apache Tomcat v6.0 is not defined. phyy Unknown Faceted Project Problem

    Description Resource Path Location TypeTarget runtime Apache Tomcat v6.0 is not defined. phyy Unknow ...

  5. Target runtime Apache Tomcat v8.0 is not defined.

    Target runtime Apache Tomcat v8.0 is not defined. Window-Preference-MyEclipse-Targeted Runtimes,选择存在 ...

  6. 【eclipse】Target runtime Apache Tomcat v7.0 is not defined解决

    在eclipse中导入项目时提示Target runtime Apache Tomcat v7.0 is not defined, 解决方法:右键项目--properties--targeted ru ...

  7. 创建Dynamic Web Project时 显示最新Apache Tomcat 8.0 的方法

    创建Dynamic Web Project时  显示最新Apache Tomcat  8.0 等的方法 解决办法如下: 第一步:eclipse菜单help->eclipse marketplac ...

  8. Server Library [Apache Tomcat 7.0] unbound解决方案

    问题描述: 当在MyEclipse中导入高版本Eclipse的[Eclipse Dynamic Web]项目后,会发现其Java Build Path(选定项目->Alt+Enter即可打开Pr ...

  9. 发现eclipse红叉,查看markers发现Target runtime Apache Tomcat 6.0 is not defined

    1.导入以前的项目(Markers中注意查看,就在console选项卡旁边),报以下错误,但不影响操作: Description Resource Path Location TypeTarget r ...

随机推荐

  1. 洗清UI自动化鸡肋说的不白之冤

    人类文明发展的一个重要标识是工具的诞生,当人类开始制作工具来提高生产力时,就逐渐拉开了与其他生物的距离.曾在2013年,<Google如何测试软件>中提到的分层自动化金字塔,轰动业界.而在 ...

  2. iOS7 iOS8 UITableviewCell处于编辑状态,dismiss或者back崩溃

    今天在项目中遇到一个坑爹的 Crash , 在 iOS7 iOS8 UITableviewCell处于编辑状态,dismiss或者back崩溃  iOS9不会 原因:苹果的BUG代码 解决:在视图消失 ...

  3. math对象和date对象

    math对象的函数方法,记住Math首字母要大写 console.log(Math.abs(-5)); //取绝对值 console.log(Math.ceil(1.1)); //向上取舍 conso ...

  4. C#中messagebox用法

    [函数] <整型> MessageBox(<字符串 Text, <字符串> Title, <整型> nType,MessageBoxIcon);[函数说明] ...

  5. [SSH 2] 以网站主页面浅谈Struts2配置

    导读:前面总体的介绍了一下SSH框架,那么作为Struts这一支,具体是怎么配置的呢?本篇博客则主要是以自己做过的实例中的登录一条线,简单介绍一下struts2的配置,如有不妥之处,还请大家多提点提点 ...

  6. MFC读取XML文件并解析

    现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码: 首先得等在stdafx.h中加入这句,以引入MSXML命名空间 #import <msxml3.dll> ...

  7. 动态链接库(VC_Win32)

    目录 动态链接库概述相关函数动态链接库编程dumpbin工具 (本章节中例子都是用 VS2005 编译调试的) 动态链接概述 说明 所谓动态链接,就是把一些经常会共用的代码(静态链接的OBJ程序库)制 ...

  8. Leetcode026. Remove Duplicates from Sorted Array

    water class Solution { public: int removeDuplicates(vector<int>& nums) { for(vector<int ...

  9. Windbg 进程与线程 《第三篇》

    Windbg既可以显示进程和线程列表,又可以显示指定进程或线程的详细信息.调试命令可以提供比taskmgr更详尽的进程资料,在调试过程中不可或缺. 一.进程命令 进程命令包括这些内容:显示进程列表.进 ...

  10. JavaScript的检测属性、属性特性、枚举属性

    /* 检测属性 检测属性可以通过三种方式 1.通过in运算符 2.通过hasOwnPerperty() 如果给定的属性是继承属性将返回false 3.通过propertyIsEnumerable(): ...