从Servlet到Struts 2.1.6
前段时间,朋友做了一个“基于MVC的JSP+Servlet+JavaBean整合开发的例子”,有老师质疑它这个是不是真正的MVC标准?至于这个问题,我们在这里不讨论,本文目的是用Struts2.1.6来取代Servlet。经我这位朋友同意,我把他所做的那个“JSP+Servlet+JavaBean”的例子,发布给大家,大家可以跟着我的步骤,一步一步地,把这个Servlet的例子,改写成Struts2.1.6的例子。
附件下载: /Files/rongxh7/MvcModel.rar。
这是我朋友的例子,大家先下载下来,导入Eclipse-jee,并把相应的数据库文件导入SQL Server 2000,运行一下他这个小项目。如果出现什么问题,可以到他的技术博客给他留言。 http://www.blogjava.net/gdhqs。
本文重点是如何把他这个Servlet的小项目改写成Struts2.1.6的项目。首先,下载Struts2.1.6的类库, http://struts.apache.org/2.1.6/index.html, 下载下来后是这个文件:struts-2.1.6-all.zip,解压缩后,在lib目录下,找到以下必需的jar包,把它们添加到我们项目的lib目录下。这些jar有:
xwork-
2.1.2
.jar
struts2-core- 2.1.6 .jar
struts2-convention-plugin- 2.1.6 .jar
ognl- 2.6.11 .jar
freemarker- 2.3.13 .jar
commons-loggin- 1.0.4 .jar
commons-fileupload- 1.2.1 .jar
struts2-core- 2.1.6 .jar
struts2-convention-plugin- 2.1.6 .jar
ognl- 2.6.11 .jar
freemarker- 2.3.13 .jar
commons-loggin- 1.0.4 .jar
commons-fileupload- 1.2.1 .jar
我还是在我朋友的项目基础上做吧,先把“MvcModel”(他的项目名)项目下的web.xml修改一下。你可以把他配置的所有Servlet给删除掉,然后,配置是Struts2的过滤器,代码如下:
<
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 >
这样,就简洁了许多,比配置N个Servlet简洁了。
< 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 >
接着,在项目的Web-INF目录下,新建一个文件夹名为"content",这个目录名要是这个,如果要改成其它名的话,还得在相应的配置文件中配置,但初学的时候,就按它默认的吧。我们编写的JSP文件,就放在此目录下。这样做,目的之一为了提高安全性,因为在Web-INF下的所有文件,不能直接访问的。
我们可以把我朋友的那个项目的主页index.jsp文件移到content目录下,或者,自己新建一个也可以。以下是index.jsp的代码,注意,这个是我朋友项目中的有所不同了,编码改成了UTF-8.
<%
@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
< title > MVC MODEL </ title >
</ head >
< body >
< h1 > MVC MODEL </ h1 >
< ul >
< li >< a href ="people!input.action" > 添加人员 </ a ></ li >
< li >< a href ="people.action" > 人员列表 </ a ></ li >
</ ul >
</ body >
</ html >
pageEncoding="UTF-8" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=ISO-8859-1" >
< title > MVC MODEL </ title >
</ head >
< body >
< h1 > MVC MODEL </ h1 >
< ul >
< li >< a href ="people!input.action" > 添加人员 </ a ></ li >
< li >< a href ="people.action" > 人员列表 </ a ></ li >
</ ul >
</ body >
</ html >
我们在浏览器中输入地址 http://localhost:8060/MvcModel/index, 注意,我的端口号与你的可能不同,这个访问路径“index”不能带".jsp"后缀,否则会报找不到action的错误。或者,你加个".action"的后缀也可以。Struts2.1.6与Struts2.0不同之处之一是,Struts2.1.6中的“xxx.action”可以没有Action类与它对应,它如果找不到有相应的action类,它会去找xxx.jsp,xxx.htm等文件。
我们做到这一步,如果能够通过上面的路径预览index.jsp的话,就说明,Struts2.1.6配置正确了。继续下面的步骤吧。
来写一个Struts的action类,命名为"PeopleAction",代码如下:
package
cn.he.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import cn.he.manager.Manager;
import cn.he.pojo.People;
import com.opensymphony.xwork2.ActionSupport;
@Results( {
@Result(name = "reload", location = "people.action", type = "redirect")
} )
public class PeopleAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Manager manager = new Manager();
private int id;
private People people;
private List<People> peoples;
//默认的操作
@Override
public String execute() throws Exception {
return list();
}
//查询列表的操作
public String list() throws Exception {
System.out.println("list");
peoples = manager.queryAllPeople();
System.out.println("name = " + peoples.size());
return SUCCESS;
}
//进入编辑页面前的操作
public String input() throws Exception {
System.out.println("input");
return INPUT;
}
//保存操作
public String save() throws Exception {
manager.addPeople(people);
return "reload";
}
//删除操作
public String delete() throws Exception {
manager.delPeople(id);
return "reload";
}
//自动生成相应的getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
public List<People> getPeoples() {
return peoples;
}
public void setPeoples(List<People> peoples) {
this.peoples = peoples;
}
}
注意,这个类的包名“cn.he.action”,要有名为“action”或者"struts",或者"web"等,相关的命名规范请参考
struts2采用convention-plugin实现零配置,Struts2.1.6默认会把这些包下或者这些包的子包下的类,纳入自己的管理范围之内。import java.util.List;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import cn.he.manager.Manager;
import cn.he.pojo.People;
import com.opensymphony.xwork2.ActionSupport;
@Results( {
@Result(name = "reload", location = "people.action", type = "redirect")
} )
public class PeopleAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Manager manager = new Manager();
private int id;
private People people;
private List<People> peoples;
//默认的操作
@Override
public String execute() throws Exception {
return list();
}
//查询列表的操作
public String list() throws Exception {
System.out.println("list");
peoples = manager.queryAllPeople();
System.out.println("name = " + peoples.size());
return SUCCESS;
}
//进入编辑页面前的操作
public String input() throws Exception {
System.out.println("input");
return INPUT;
}
//保存操作
public String save() throws Exception {
manager.addPeople(people);
return "reload";
}
//删除操作
public String delete() throws Exception {
manager.delPeople(id);
return "reload";
}
//自动生成相应的getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
public List<People> getPeoples() {
return peoples;
}
public void setPeoples(List<People> peoples) {
this.peoples = peoples;
}
}
接着在content目录下,写两个JSP文件,一是查询列表的JSP页面:people.jsp,代码如下:
<%
@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<% @ taglib prefix="s" uri="/struts-tags" %>
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" >
< title > People列表页 </ title >
</ head >
< body >
< table >
< tr >
< td > id </ td >
< td > name </ td >
< td > delete </ td >
</ tr >
< s:iterator value ="peoples" var ="peo" >
< tr >
< td >< s:property value ="people.id" /> </ td >
< td > ${peo.name} </ td >
< td >< a href ="people!delete.action?id=${peo.id}" > 删除 </ a ></ td >
</ tr >
</ s:iterator >
</ table >
</ body >
</ html >
pageEncoding="utf-8" %>
<% @ taglib prefix="s" uri="/struts-tags" %>
<% @ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" >
< title > People列表页 </ title >
</ head >
< body >
< table >
< tr >
< td > id </ td >
< td > name </ td >
< td > delete </ td >
</ tr >
< s:iterator value ="peoples" var ="peo" >
< tr >
< td >< s:property value ="people.id" /> </ td >
< td > ${peo.name} </ td >
< td >< a href ="people!delete.action?id=${peo.id}" > 删除 </ a ></ td >
</ tr >
</ s:iterator >
</ table >
</ body >
</ html >
另一个添加人员的页面:people-input.jsp,代码如下:
<%
@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<% @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" >
< title > People添加页 </ title >
</ head >
< body >
< form action ="people!save.action" method ="post" >
名字: < input type ="text" name ="people.name" /> < br />
< input type ="submit" value ="添加" />
</ form >
</ body >
</ html >
pageEncoding="utf-8" %>
<% @ taglib prefix="s" uri="/struts-tags" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" >
< title > People添加页 </ title >
</ head >
< body >
< form action ="people!save.action" method ="post" >
名字: < input type ="text" name ="people.name" /> < br />
< input type ="submit" value ="添加" />
</ form >
</ body >
</ html >
运行一下看看效果吧!
本文原创,转载请注明出处,谢谢!http://www.blogjava.net/rongxh7(心梦帆影JavaEE技术博客)