环境准备:
1、Tomcat 6.0.20 (我用的是Windows安装版)
2、Eclipse_JEE_Galileo
3、Struts 2.0
4、Tomcat 插件
配置:
1、把 Tomcat 插件 集成到 Eclipse 里。解压好的 Tomcat 插件 (com.sysdeo.eclipse.tomcat_3.2.1) 目录拷贝到 {Eclipse_JEE_Galileo}\plugins 目录下,重启 Eclipse。
2、设置 Tomcat 插件
如果需要 DEBUG JAVA程序,要在上图 Tomcat 标签下的 JVM Settings 标签中设置。
在 Append to JVM Parameters 里追加:
-Xdebug
-Xnoagent
-Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,address=1155,suspend=n,server=y
如图:
3、新建一个Struts 工程:
工程建完的的结构为:
把Struts 2.0 要用的类包拷贝到 WebContent -> WEB-INF -> lib 目录下:
修改 WEB-INF 下的 web.xml 文件为:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Hello Struts 2.0</display-name>
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
在 WebContent 目录下添加 HelloWorld.jsp, index.html, SayHello.jsp:
HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="name"/></h3>
</body>
</html>
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h3>Hello World!</h3>
</body>
</html>
SayHello.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Say Hello</title>
</head>
<body>
<h3>Say "Hello" to:</h3>
<s:form action="HelloWorld">
Name: <s:textfield name="name" />
<s:submit />
</s:form>
</body>
</html>
在 src 目录下,新建 structs.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="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
在 src 目录下追加 tutorial 目录,tutorial 目录加追加 HelloWorld.java 文件,内容如下:
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
name = "Hello " + name + "!";
return SUCCESS;
}
}
然后发布:
点击 Next:
发布成功之后,启动 Tomcat。如果成功的话,在 IE 地址栏上输入:
http://localhost:8080/HelloTest/SayHello.jsp ,会出去 SayHello.jsp 页面。
输入一串字符,点 Submit 按钮,会进入 HelloWorld.jsp 页面。因为本文主要想说明怎么配置热部署的工程,所以对于工程的代码及工程建立过程不做太多说明了。有问题请上网查吧。
4、远程监控(Debug):
如果在 Tomcat 的 bin 目录下直接启动 Tomcat,那么要远程监控的话,还要设置上面在配置 Tomcat 插件时的参数。如果是用的解压缩版的 Tomcat 6.0,不是安装版的,那设置方法请上网查,网上都是说明的解压缩版 Tomcat 6.0 的设置方法。
点击 {Tomcat 6.0}\bin\tomcat6w.exe,如下图:
如果是在 Eclipse_JEE_Galileo 里启动的 Tomcat 插件,那么 Tomcat 不用做什么设置了。下面,我们来设置 Debug 的启动:
追加一个 Remote Java Application,起名叫 HelloTest,工程选我们的工程 HelloTest,主要设置一下端口,这里是1155,这个端口就是我们上面设 Tomcat (插件或正常启动) 时候加的那个 "address=1155",就是这里要设定的端口值。如图:
当我们启动 Tomcat 以后,就可以在 Java 代码里打断点了。
5、设置热部署:
这是我要说的最重要的地方,网上很少有资料说明这个东西,也可能是我找的方法不对。首先,我们如果只是按以上方法进行“发布”,“设置”的话,那么,每改一点 Java 代码或者 jsp 代码,都要重新发布,才能看见结果,这样显然很麻烦。我们希望用一种方法,让我们每次更改完代码,直接就能反正到服务器上去。
那么,让我们来设置一下吧。
首先,我们先在 {Tomcat 6.0}\conf\Catalina\localhost\ 目录下,新建一个 HelloTest.xml,很显然,新建的 xml 文件名就是我们的工程名。HelloTest.xml 的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Context docBase="E:\Program Files\eclipse\workspace\HelloTest\WebContent" reloadable="true" antiResourceLocking="false" privileged="true" />
把 Context 的 docBase 属性指到我们工程的 WebContent 下。这样,服务器就可以去我们的 WebContent 目录下找到我们需要的文件(class,jsp)。不过,现在还有点问题。如果我们现在重启一下 Tomcat,那么,在我们修改WebContent 下的 SayHello.jsp后,刷新一下,就可以反应到页面上了。可是点 Submit 后,会报错。
我们比较一下,{Tomcat 6.0}\webapps\HelloTest\WEB-INF 目录和我们工程目录下的 WebContent 目录,看看有什么不一样,我们会发现,我们的工程目录 WebContent 下,没有classes 目录,即没有工程中的 src 目录下的东西(struts.xml 和 Java 编译好的 class)。那怎么办呢?我们还要修改一下工程(HelloTest)目录下的 .classpath 文件,把原来的
<classpathentry kind="output" path="build/classes"/>
改为
<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
让工程先自动编译到 WebContent/WEB-INF/classes 目录下。保存后刷新,再重启 Tomcat 服务器,这回就没有问题了,我们再修改 jsp 页面或是 Java 代码,也不用重新发布了,只需要刷新一下就可以了。等开发完成后,再把原来的.classpath 文件改回去即可。 |