软件:
JavaJDK、apache-tomcat-7.0.8-windows-x86、myeclipse.9.0.M2发布].myeclipse-9.0M2-offline-installer-windows、MySQL
MyEclipse与Tomcat集成:
首先,要确保javajdk已经安装好了然后,分别解压Tomcat和安装MyEclipse软件。启动MyEclipse,Windows--->Preferences-->MyEclipse--->Servers--->
Tomcat--->Tomcat 7.x双击得到下面的图形:
将Tomcat homedirectory设定为我们放置解压后的Tomcat文件的地方.我们还可以设置Tomcat7.x下运行的jdk,一般采用默认的不用我们来修改.
测试Tomcat是否与MyEclipse集成成功:
我们通过点击MyEclipse工具栏中的工具来选择启动Tomcat7服务器
然后,在浏览器中输入http://localhost:8080/,如果配置成功的话,则出现Tomcat首页
开发第一个Struct2程序:
下载struts2:
要想使用最新的或者是现阶段比较流行的struts版本我们需要自己上网去http://struts.apache.org/下载,解压后可以看到有以下内容
apps:包含基于struts2的示例应用
docs:包含struts2的相关文档
lib:包含struts2框架的核心类库,以及strut2的第三方插件类库
src:包含struts2框架的全部源代码
创建Web Project,命名项目的名称为strut
加载struts2包:
将xwork-2.0.7,tiles-jsp-2.0.4,struts2-core-2.0.14,ognl-2.6.11,
,freemarker-2.3.8,commons-logging-1.0.4复制到刚刚创建的项目下的WEB-INF-->lib文件夹下.然后选中工程右击选择BuildPath--->Configure Build Path出现下图对话框,单击Add ExternalJARS,再将上面的这5个包添加到项目中
4.修改Web.xml文件
打开WebRoot/WEB-INF/web.xml修改为
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
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>strut</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>strut</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5.创建login.jsp
右击WebRoot--->new--->jsp
<%@ pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'login.jsp' startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is mypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<formaction="login.action"method="post">
username: <inputname="username"type="text"><br>
password: <inputname="password"type="password"><br>
<inputtype="submit"value="submit">
</form>
</body>
</html>
6.实现控制器
在src目录下创建一个新类LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
public String execute()
{
if(getUsername().equals("chne")&&getPassword().equals("lovejwj2004"))
return "success";
else
return "error";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Tips:username和passwrod的命名必须与login.jsp中使用的文本输入框的命名严格保持一致.
7.配置struts.xml
在src下生成文件struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="strut"extends="struts-default">
<actionname="login"class="LoginAction">
<!-- result没有名字是默认的success -->
<resultname="success">/welcome.jsp</result>
<resultname="error">/error.jsp</result>
</action>
</package>
</struts>
8.在WebRoot下创建welcome.jsp和error.jsp页面
welcome.jsp:
<%@ pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%@ taglibprefix="s"uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'welcome.jsp'starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is mypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<s:propertyvalue="username"/>welcome to log in
</body>
</html>
error.jsp:
<%@ pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'error.jsp' startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is mypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
Fail to log in for invalidate username orpassword<br>
</body>
</html>
9.部署
将应用部署到服务器上,启动tomcat,运行,在浏览器中输入http://localhost:8080/strut/login.jsp
Q:使用tomcat的时候可能会报出异常Error starting static Resources.
导致上面的错误信息:
(1)我原来工程有个叫“photo”的web工程,我现在把移除掉了。
(2)但是,我在Tomcat的server.xml文件中配置了虚拟目录,也即是如下信息
现在原因已经找到,Tomcat中“photo”已经不存在,但是server.xml文件中还保留虚拟目录信息,所以直接导致Tomcat报如下错误
Java代码
严重: Error starting static Resources
java.lang.IllegalArgumentException: Document baseE:\apache-tomcat-5.5.26\webapps\photo does not exist or is not areadable directory atorg.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:141)
ok,如果你把server.xml那段信息删除,重新启动Tomcat就没有问题了。