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. Appium + Python -------------元素定位

    说在前面 1.https://github.com/appium/python-client/tree/master/test  里面有一些test ,可以看看,研究研究 2.学会使用 uiautom ...

  2. git添加文件过滤

    操作流程 touch .gitignore vi .gitignore,添加需要过滤的文件或目录 git commit 出现问题 假如.gitignore里面添加file,而git库中已经存在file ...

  3. 个人收集(转载)CSS中 display:none和visibility:hidden的区别

    visibility和display两个属性都有隐藏元素的功能,display:none和visibility:hidden的区别,简单的总结一句话就是:visibility:hidden隐藏,但在浏 ...

  4. Debian的一个命令

    dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...

  5. shell学习笔记(1):利用IFS打印用户和默认shell

    参考资料为:linux shell脚本攻略 作者sarath Lakshman 人民邮电出版社 shell:读取文件的每一行内容并输出 的写法1 目的:读取passwd文件,获得用户名和其默认的she ...

  6. 【C++面试】常考题复习:排序算法

    // Sort.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> /*********** ...

  7. 将windows系统装到USB存储设备

    需求: 1)一般公司比较规范,计算机系统有严格的限制策略,如果自己不懂得如何更改或者没有权限更改,将极其不便. 2)计划在家里完成在公司未完成的事,甚至异地出差觉得携带笔记本不太方便,寻找更便携的设备 ...

  8. JSF 监听

    JSF项目中实现基于RBAC模型的权限管理设计(二) 转 4.3 权限验证模块设计 一个好的权限管理机制在项目中应用时,最好不要让程序员在具体业务代码的方法中来判断用户权限.因为这意味着大量重复的代码 ...

  9. MySQL允许远程访问

    grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; flush privileges; ...

  10. sublime text 2 中文乱码解决办法

    sublime text 2是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦.可以通过向sublime text 中添加编码类型转换包(比如 ...