SSH整合系列1--配置Struts2.1.8.1,并测试第一个action
系统平台:
Struts:2.1.8.1
Spring:3.0.3
Hibernate:3.5.2
tomcat:6.0.26
MyEclipse:7.5
Ⅰ.配置Struts2.1.8.1:
1.下载最新的Struts2.1.8.1
2.建立web工程wdpweb
3.引入必须包
4.在web.xml中配置FilterDispatcher过滤器
5.配置struts.xml
1.下载最新的Struts2.1.8.1
Struts作为Apache的一个项目,当然是去Apache的官网下载。这里提到一本书,是官网推荐的《Stating Struts2》,并有中文版下载《深入浅出Struts2》,可以在Struts2首页面的Documentation栏目进行下载(下载前需要注册),对于学习和使用Struts2非常有帮助。
2.建立web工程wdpweb
3.引入必须包
将Struts2.1.8.1中lib文件夹下的六个jar包:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ongl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
拷贝到web工程中的/WEB-INF/lib目录中。
说明:在以前,一般只需要后五个jar包,但是在2.1.8.1版本中,如果没有commons-fileupload-1.2.1.jar,tomcat启动会报如下错误:
Java代码
2010-3-10 16:28:31 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter Struts2
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Context [/wdpweb] startup failed due to previous errors
2010-3-10 16:28:31 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter Struts2
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Context [/wdpweb] startup failed due to previous errors
4.在web.xml中配置FilterDispatcher过滤器
FilterDispatcher是一个servlet过滤器,它是Struts2整个Web应用的配置项,需要在“web.xml”中进行配置。
代码如下:
Xml代码
<filter>
<!-- filter-name可以自定义,但是要保证与下面filter-mapping中的filter-name一致 -->
<!-- 这样才保证了filter和filter-mapping的一一对应 -->
<filter-name>Struts2</filter-name>
<!-- 这个核心拦截器FilterDispatcher可以在struts2-core包中轻松找到 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- FilterDispatcher -->
<filter>
<!-- filter-name可以自定义,但是要保证与下面filter-mapping中的filter-name一致 -->
<!-- 这样才保证了filter和filter-mapping的一一对应 -->
<filter-name>Struts2</filter-name>
<!-- 这个核心拦截器FilterDispatcher可以在struts2-core包中轻松找到 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- FilterDispatcher -->
5.配置struts.xml
在 src 目录下新建 struts.xml 文件,注意不是 WEB-INF目录!
书写DTD:可以从struts2 core包中的 struts-default.xml 文件中将DTD拷贝出来
Xml代码
<!-- DTD:文档类型定义(Document Type Definition) -->
<!-- XML的文档类型定义。提供了XML文件中各个元素所使用结构信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- DTD:文档类型定义(Document Type Definition) -->
<!-- XML的文档类型定义。提供了XML文件中各个元素所使用结构信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
然后在<struts></struts>这个大标签中书写package标签:
Xml代码
<!-- name:package的唯一标示,自定义;
extends:指明本package所继承的包,一般继承struts-default.xml;
namespace:命名空间提供了从URL到Package的映射
-->
<package name="Struts2" extends="struts-default" namespace="/">
<!-- action -->
</package>
<!-- name:package的唯一标示,自定义;
extends:指明本package所继承的包,一般继承struts-default.xml;
namespace:命名空间提供了从URL到Package的映射
-->
<package name="Struts2" extends="struts-default" namespace="/">
<!-- action -->
</package>
其中,namespace指明了程序发布后,在url中的映射路径,如:
namespace="/",浏览器路径:http://localhost:8080/wdpweb/***.action
namespace="/pak1",浏览器路径:http://localhost:8080/wdpweb/pak1/***.action
namespace="/pak2",浏览器路径:http://localhost:8080/wdpweb/pak2/***.action
Ⅱ.第一个action
1.配置action
2.创建JSP文件 HelloWorld.jsp
3.创建 HelloWorld.java
1.配置action
在struts.xml的package标签中,编写如下action
Xml代码
<!-- hello action -->
<action name="helloworld" class="com.hust.wdp.helloworld.HelloWorld">
<result>/WEB-INF/HelloWorld.jsp</result>
</action>
<!-- hello action -->
<action name="helloworld" class="com.hust.wdp.helloworld.HelloWorld">
<result>/WEB-INF/HelloWorld.jsp</result>
</action>
2.创建JSP文件 HelloWorld.jsp
在WEB-INF目录下创建HelloWorld.jsp,要使用Struts标签,就必须引入标签库,在文件开头编写:
Html代码
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
html的body内容为:
Html代码
<body>
Hello World! This is the first JSP page!<br>
<br/>
<s:property value="message"/>
</body>
<body>
Hello World! This is the first JSP page!<br>
<br/>
<s:property value="message"/>
</body>
3.创建 HelloWorld.java
在包路径com.hust.wdp.helloworld下,创建HelloWorld.java。代码如下:
Java代码
package com.hust.wdp.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5351576682976847552L;
private String message = "Hello World! Struts2 is taking function!";
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.setMessage(message);
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.hust.wdp.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5351576682976847552L;
private String message = "Hello World! Struts2 is taking function!";
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.setMessage(message);
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4.测试
在MyEclipse中启动tomcat,在浏览器中输入http://localhost:8080/wdpweb/helloworld.action (可以不写 .action),即看到如下结果:
Hello World! This is the first JSP page!
Hello World! Struts2 is taking function!
说明:在浏览器地址中的wdp为项目名称,因为tomcat会将所有web工程部署在自身的webapps目录下,所以url中应当出现“/wdpweb”路径,如果在上面的package标签中定义了namespace,则需在url中出现“/wdpweb/namespace”。
系统平台:
Struts:2.1.8.1
Spring:3.0.3
Hibernate:3.5.2
tomcat:6.0.26
MyEclipse:7.5
Ⅰ.配置Struts2.1.8.1:
1.下载最新的Struts2.1.8.1
2.建立web工程wdpweb
3.引入必须包
4.在web.xml中配置FilterDispatcher过滤器
5.配置struts.xml
1.下载最新的Struts2.1.8.1
Struts作为Apache的一个项目,当然是去Apache的官网下载。这里提到一本书,是官网推荐的《Stating Struts2》,并有中文版下载《深入浅出Struts2》,可以在Struts2首页面的Documentation栏目进行下载(下载前需要注册),对于学习和使用Struts2非常有帮助。
2.建立web工程wdpweb
3.引入必须包
将Struts2.1.8.1中lib文件夹下的六个jar包:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ongl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
拷贝到web工程中的/WEB-INF/lib目录中。
说明:在以前,一般只需要后五个jar包,但是在2.1.8.1版本中,如果没有commons-fileupload-1.2.1.jar,tomcat启动会报如下错误:
Java代码
2010-3-10 16:28:31 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter Struts2
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Context [/wdpweb] startup failed due to previous errors
2010-3-10 16:28:31 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter Struts2
Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load configuration. - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/D:/apache-tomcat-5.5.27/apache-tomcat-5.5.27/webapps/wdpweb/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
……
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext
……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
……
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2010-3-10 16:28:31 org.apache.catalina.core.StandardContext start
严重: Context [/wdpweb] startup failed due to previous errors
4.在web.xml中配置FilterDispatcher过滤器
FilterDispatcher是一个servlet过滤器,它是Struts2整个Web应用的配置项,需要在“web.xml”中进行配置。
代码如下:
Xml代码
<filter>
<!-- filter-name可以自定义,但是要保证与下面filter-mapping中的filter-name一致 -->
<!-- 这样才保证了filter和filter-mapping的一一对应 -->
<filter-name>Struts2</filter-name>
<!-- 这个核心拦截器FilterDispatcher可以在struts2-core包中轻松找到 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- FilterDispatcher -->
<filter>
<!-- filter-name可以自定义,但是要保证与下面filter-mapping中的filter-name一致 -->
<!-- 这样才保证了filter和filter-mapping的一一对应 -->
<filter-name>Struts2</filter-name>
<!-- 这个核心拦截器FilterDispatcher可以在struts2-core包中轻松找到 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- FilterDispatcher -->
5.配置struts.xml
在 src 目录下新建 struts.xml 文件,注意不是 WEB-INF目录!
书写DTD:可以从struts2 core包中的 struts-default.xml 文件中将DTD拷贝出来
Xml代码
<!-- DTD:文档类型定义(Document Type Definition) -->
<!-- XML的文档类型定义。提供了XML文件中各个元素所使用结构信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- DTD:文档类型定义(Document Type Definition) -->
<!-- XML的文档类型定义。提供了XML文件中各个元素所使用结构信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
然后在<struts></struts>这个大标签中书写package标签:
Xml代码
<!-- name:package的唯一标示,自定义;
extends:指明本package所继承的包,一般继承struts-default.xml;
namespace:命名空间提供了从URL到Package的映射
-->
<package name="Struts2" extends="struts-default" namespace="/">
<!-- action -->
</package>
<!-- name:package的唯一标示,自定义;
extends:指明本package所继承的包,一般继承struts-default.xml;
namespace:命名空间提供了从URL到Package的映射
-->
<package name="Struts2" extends="struts-default" namespace="/">
<!-- action -->
</package>
其中,namespace指明了程序发布后,在url中的映射路径,如:
namespace="/",浏览器路径:http://localhost:8080/wdpweb/***.action
namespace="/pak1",浏览器路径:http://localhost:8080/wdpweb/pak1/***.action
namespace="/pak2",浏览器路径:http://localhost:8080/wdpweb/pak2/***.action
Ⅱ.第一个action
1.配置action
2.创建JSP文件 HelloWorld.jsp
3.创建 HelloWorld.java
1.配置action
在struts.xml的package标签中,编写如下action
Xml代码
<!-- hello action -->
<action name="helloworld" class="com.hust.wdp.helloworld.HelloWorld">
<result>/WEB-INF/HelloWorld.jsp</result>
</action>
<!-- hello action -->
<action name="helloworld" class="com.hust.wdp.helloworld.HelloWorld">
<result>/WEB-INF/HelloWorld.jsp</result>
</action>
2.创建JSP文件 HelloWorld.jsp
在WEB-INF目录下创建HelloWorld.jsp,要使用Struts标签,就必须引入标签库,在文件开头编写:
Html代码
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
html的body内容为:
Html代码
<body>
Hello World! This is the first JSP page!<br>
<br/>
<s:property value="message"/>
</body>
<body>
Hello World! This is the first JSP page!<br>
<br/>
<s:property value="message"/>
</body>
3.创建 HelloWorld.java
在包路径com.hust.wdp.helloworld下,创建HelloWorld.java。代码如下:
Java代码
package com.hust.wdp.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5351576682976847552L;
private String message = "Hello World! Struts2 is taking function!";
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.setMessage(message);
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.hust.wdp.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5351576682976847552L;
private String message = "Hello World! Struts2 is taking function!";
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.setMessage(message);
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4.测试
在MyEclipse中启动tomcat,在浏览器中输入http://localhost:8080/wdpweb/helloworld.action (可以不写 .action),即看到如下结果:
Hello World! This is the first JSP page!
Hello World! Struts2 is taking function!
说明:在浏览器地址中的wdp为项目名称,因为tomcat会将所有web工程部署在自身的webapps目录下,所以url中应当出现“/wdpweb”路径,如果在上面的package标签中定义了namespace,则需在url中出现“/wdpweb/namespace”。