基本知识:Struts2框架实现的是MVC(Model View Controller)模式,在Struts2的模式中Model、View、Controller分别由Action、Result、FilterDispatcher实现
controller的工作是将用户的请求映射到合适的action上,在Struts2中filterdispatcher做的是controller的工作,model包含数据和事务逻辑,在Struts2中由action来实现,view是MVC模式中的展示组件,在Struts2中view通常由JSP等技术实现。
开发环境:
Eclipse IDE for Java EE Developers(下载地址)
struts-2.3.1.2(下载地址)
apache-tomcat-6.0.35(下载地址)(推荐使用6.0,5.0用起来有点问题)
结果图:
开发过程:
(1)首先配置Tomcat
Windows-Preferences-Sever-Runtime Environment
(2)建立工程 - Dynamic Web Project
在点击Finish的页面上,记得在生成Web.xml上面打上勾√
(3)导入Struts2需要的包
在struts-2.3.1.2中有8个必须的包
分别是:
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
commons-io-2.0.1.jar (java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils)
commons-lang-2.5.jar ( java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils)
commons-fileupload-1.2.2.jar (java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileUploadException)
javassist-3.11.0.GA.jar (java.lang.ClassNotFoundException: javassist.ClassPool)
(4)修改原始的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>struts2_20120311_01</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.jsp</welcome-file>
</welcome-file-list>
</web-app>
web-app的头部不用修改,只修改里面的filer和welcome-file-list
FilterDispatcher是Struts2框架的核心控制器,该控制器作为一个Filter运行在Web应用中,它负责拦截所有的用户请求,当用户请求到达时,该Filter会过滤用户请求。Struts2框架获得了*.action请求后,将根据*.action请求的*部分决定调用哪个业务逻辑。(摘自《Struts2权威指南》)如下的HelloWorld.Action。
(5)编写index.jsp和success.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld">
<s:textfield name="userName" label="User Name" />
<s:submit />
</s:form>
</body>
</html>
success.jsp如下:
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
<s:property value="message" />
</h1>
</body>
</html>
(6)编写HelloWorld.java(struts2中,action的实现类可以继承ActionSupport 也可以不继承ActionSupport,此处没有继承)
package com.zeph.struts2;
public class HelloWorld {
private String message;
private String userName;
public HelloWorld() {
}
public String execute() {
setMessage("Hello " + getUserName());
return "SUCCESS";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
业务控制器组件就是用户实现Action类的实例,Action类里通常包含一个execute方法,该方法返回一个字符串——该字符串就是一个逻辑视图名,当业务控制器处理完用户请求后,根据处理结果不同,execute方法返回不同字符串——每个字符串对应一个视图名。(摘自《Struts2权威指南》)
(7)编写struts.xml
配置文件中名为“default”的包空间,继承了名为struts-default的包空间,struts-default包空间定义在struts-default.xml文件中,struts2框架默认会加载struts-default.xml文件。
在struts-default的包空间里定义了struts2内建的Result类型,还定义了Struts2内建的系列拦截器,以及有不同拦截器组成的拦截器栈。(摘自《Struts2权威指南》)
<?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>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="com.zeph.struts2.HelloWorld">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
Struts2应用中的Action都被定义在struts.xml文件中,在该文件中定义Action时,定义了Action的name属性和class属性,其中name属性决定了该Action处理哪个用户请求,而class属性决定了该Action的实现类。(摘自《Struts2权威指南》)如上name=HelloWorld表明这个Action处理HelloWorld.Action。class对应的内容就是Action的实现类HelloWorld.java。
