运行不了可能问题(后面附有一个成功的代码):
1、 workspace目录之中只能存在英文,不能包含数字、空格和中文;
2、 建web工程之前,打开C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps目录,删除与你同名的工程。
3、 注意新创建的web工程的web.xml文件开头与struts框架需要的web.xml开头是不一样的。切忌注意!!!
实验内容和步骤:
1.在MyEclipse 中创建Web工程(假设工程名ssh1)。
2.(Step-1)在MyEclipse里添加Struts2依赖的库文件(本课程还提供Struts 2.0.11版本,它需要依赖5个库文件,分别是
struts2-core-2.0.11.jar
xwork-2.0.4.jar
ognl-2.6.11.jar
freemarker-2.3.8.jar
commons-logging-1.0.4.jar)
放入WEB-INF/lib目录下。
(Step-2) 在WEB-INF/web.xml中,添加Struts2的MVC启动框架的过滤器配置(以Struts2.1.6为例):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
说明和注意的问题:
当Struts 2的版本 < 2.1.3时,过滤器的类名(filter-class)是org.apache.struts2.dispatcher.FilterDispatcher,
而当Struts 2的版本>=2.1.3后,过滤器的类名(filter-class)改为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExec
helloworld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'helloworld.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="hello2.action" method="post">
姓名:<input type="text" name="username">
<input type="submit" value="确定">
</form> <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
访问成功!!! <br>
</body>
</html>web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package helloWorld;
HelloWorldAction.java
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
String username;
public String getUsername(){
return this.username;
}
public void setUsername(String username){
this.username=username;
}
public String execute() throws Exception {
return SUCCESS;
}
}
uteFilter。-----------(转自朱老师的课件)