`
happmaoo
  • 浏览: 4325014 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

玩玩Spring之hibernate+ webwork+ spring添删改查示例

阅读更多
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
  看本文的朋友请先看上一篇文章:玩玩Spring之struts+hibernate+spring添删改查示例,否则会看不懂滴(的)。
  该示例程序的运行效果先睹为快:
  新同学:听说Struts有很多致命的技术缺陷啊,是吗?
  大牛:恩,专家都说了。
  新同学:听说J2EE程序员很讲究艺术及高雅,能举个例子吗?
  大牛:对啊,MVC中webwork就是高雅啊,用他写起程序来真是赏心悦目,华丽得真是没法容易。
  主演:webwork
  配角:struts、jsp、hibernate、spring等
  
  借着吹spring的风,今天笔者给大家介绍一种很多人都非常欣赏的轻量极、高雅的J2EE组合,那就是hibernate+ webwork+ spring。说介绍不准确,应该还只是一个简单的演示,毕竟webwork的赞歌已经有很多人唱过了,这里就不重复(主要还是因为唱不出来,惭愧!)。我只希望能通过简单的代码,让大家去体会webwork的高雅、过人之处。
  同样是上一篇中有关一个用户表“添删改查”的例子,这里只是把Web层改成使用webwork实现,下面我们直接介绍Web开发部分,其它部分的开发请照搬前面一篇的例子,新人可以结合上个例子中的Struts部分进行看。

1、写Action

  webwork跟Struts乃至其它的MVC框架一样,都有一个处理Web请求的Action,我们来看看本例子中这个Action的内容。下面是UserManageAction.java的全部代码:

package com.easyjf.webwork.action;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.easyjf.example.business.IUser;
import com.easyjf.example.business.IUserService;
import com.easyjf.util.CommUtil;
import com.easyjf.web.tools.IPageList;
import com.easyjf.web.tools.ListQuery;
import com.easyjf.web.tools.PageList;
import com.opensymphony.webwork.interceptor.ParameterAware;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Preparable;
public class UserManageAction extends ActionSupport implements ParameterAware,Preparable {
private List allUser;
private IUserService userService;
private IUser user;
private Map parameters;
//用户查询
public String query() throws Exception {
String[] temp=(String[])parameters.get("page");
int currentPage=CommUtil.null2Int(temp!=null?temp[0]:"0");
temp=(String[])parameters.get("pageSize");
int pageSize=CommUtil.null2Int(temp!=null?temp[0]:"0");
if(currentPageif(pageSizeString scope="1=1";
Collection paras=new ArrayList();
temp=(String[])parameters.get("orderType");
String orderType=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("orderField");
String orderField=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("queryUserName");
String userName=CommUtil.null2String(temp!=null?temp[0]:"");
temp=(String[])parameters.get("queryTel");
String tel=CommUtil.null2String(temp!=null?temp[0]:"");
if(!userName.equals(""))
{
scope+=" and userName like ?";
paras.add("%"+userName+"%");
}
if(!tel.equals(""))
{
scope+=" and tel like ?";
paras.add("%"+tel+"%");
}
if(orderField.equals(""))//默认按用户名排序
{
orderField="userName";
orderType="desc";
}
if(!orderField.equals(""))
{
scope +=" order by "+orderField;
if(!orderType.equals(""))scope+=" "+orderType;
}
IPageList pList=new PageList(new ListQuery(userService.query(scope,paras)));
pList.doList(pageSize,currentPage,"","");
if(pList!=null){
this.parameters.put("list",pList.getResult());
this.parameters.put("pages",new Integer(pList.getPages()));
this.parameters.put("rows",new Integer(pList.getRowCount()));
this.parameters.put("page",new Integer(pList.getCurrentPage()));
this.parameters.put("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
}
this.parameters.put("orderType",orderType);
this.parameters.put("orderField",orderField);
this.allUser = userService.query(scope,paras);
return execute();
}
//添加或修改用户
public String edit() throws Exception
{
return execute();
}
//删除用户
public String del() throws Exception
{
if(user!=null && userService.del(this.user))
{
this.parameters.put("msg","数据删除成功!");
}
else
{
this.parameters.put("msg","数据修改失败");
}
return query();
}
//保存用户信息
public String save() throws Exception
{
IUser obj=this.getUser();
boolean ret=false;
if(obj!=null){
if((obj.getCid()!=null)&&(!obj.getCid().equals("")))ret=userService.update(user);
else ret=userService.save(user);
}
if(ret)
{
this.parameters.put("msg","数据操作成功!");
}
else
{
this.parameters.put("msg","数据操作失败");
}
return query();
}
public IUserService getUserService() {
return userService;
}
public List getAllUser() {
return allUser;
}
public IUser getUser() {
return user;
}
public void setUser(IUser user) {
this.user = user;
}
public void setUserService(IUserService userService)
{
this.userService=userService;
}
public void setParameters(Map parameters) {
this.parameters=parameters;
}
public Map getParameters() {
return parameters;
}
public void prepare() throws Exception {
this.form2Obj();
}
//根据页面参数创建IUser对象
private void form2Obj() {
String[] temp=(String[])parameters.get("cid");
String cid=temp!=null?temp[0]:"";
IUser user=null;
if(cid!=null && (!cid.equals("")))user=userService.read(cid);
if(user==null)user=userService.newUser();
setUser(user);
}
}
2、写页面文件
  xwork支持多种页面模板,可以是JSP,也可以是Freemarker等,这里我们使用的是大家比较熟悉的JSP作模板。页面同样分为userEdit.jsp以及userList.jsp两个,模板中使用了一些wework的标签,(还好没有全部搞成标签,呵呵!),大家注意观察以<ww:><div></div> <div><div>  <a href="http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115" target="_blank"><font color="#0000ff">http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115</font></a> </div></div> <div></div> <div><strong>3、配置xwork.xml</strong></div> <div></div> <div> <br>  可以理解成struts中的struts-config.xml文件,当然等你真正熟悉他以后,发现他的配置文件中的玩艺及技术含量都要比Struts牛的多。下面是xwork.xml文件的全部内容:</div> <div> <br>  ttp://www.opensymphony.com/xwork/xwork-1.1.1.dtd"&gt;<font color="#000033">http://www.opensymphony.com/xwork/xwork-1.1.1.dtd</font>"&gt;<br><xwork><br><include file="webwork-default.xml"></include><br><package name="userManage" extends="webwork-default" namespace="/userManage"><br><action name="query" class="com.easyjf.webwork.action.UserManageAction" method="query"><br><result>/userList.jsp</result><br><interceptor-ref name="params"></interceptor-ref><br><interceptor-ref name="basicStack"></interceptor-ref><br></action><br><action name="edit" class="com.easyjf.webwork.action.UserManageAction" method="edit"><br><result>/userEdit.jsp</result><br><interceptor-ref name="params"></interceptor-ref><br><interceptor-ref name="basicStack"></interceptor-ref><br></action><br><action name="save" class="com.easyjf.webwork.action.UserManageAction" method="save"><br><result name="input">/userEdit.jsp</result><br><result>/userList.jsp</result><br></action><br><action name="del" class="com.easyjf.webwork.action.UserManageAction" method="del"><br><result>/userList.jsp</result><br></action><br></package><br></xwork> </div> <div></div> <div></div> <div><strong>4、修改web.xml文件<br></strong></div> <div>  以Struts类似,同样需要修改web.xml文件,才能让webwork处理指定的请求。下面是本例中的web.xml文件的全部内容:</div> <div> <br><?xml version="1.0" encoding="ISO-8859-1"?><br>ttp://java.sun.com/dtd/web-app_2_3.dtd"&gt;<font color="#000033">http://java.sun.com/dtd/web-app_2_3.dtd</font>"&gt;<br><web-app><br><filter><br><filter-name>webwork</filter-name><br><filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class><br></filter><br><filter-mapping><br><filter-name>webwork</filter-name><br><url-pattern>/*</url-pattern><br></filter-mapping><br><listener><br><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><br></listener><br></web-app> </div> <div> <br>  通过上面的配置,使得所有的请求都会交给webwork处理。</div> <div></div> <div>  好,现在一个完整的webwork示例程序就完了。不知webwork新朋友注意到没,webwork中没有了ActionForm Bean,配置文件也相对比较清晰明了,Action中的方法不再有类似public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)这样一大串让人看起来就不爽的参数,其干净得一个参数也没有。大家可以闭上眼睛摸着脑门再体会一下Webwork的Action的写法,是不是要简洁、华丽、优雅得多。</div> <div></div> <div>  当然,那么多技术牛人在支持webwork,并不是单看中这个Action的简洁,webwork本身还有很多东西,还包括AJAX支持,报表集成,强大的验证系统以及IOC等,其中有很多是Spring也有的东西,仅从技术的角度来看,笔者也认为他确实能称上是数一数二的。</div> <div></div> <div>  当然,本人不是webwork的粉丝,因此其它MVC框架的粉丝这里不要PK我,Struts的朋友就更不应该了,因为webwork开发团队与struts团队已经合并了,合并后的作品Apache Struts Action 2.0也即将出来了。Tapestry 、JSF的粉丝看到这里也别纳闷,有竞争才有技术进步嘛。<br>  <br>  好,今天就先玩到这里。由于笔者对webwork只是粗浅的看了一下,因此也许还不能完全通过这个示例表述出webwork的原意。还请webwork的超级粉丝或专家来帮忙补充一下,若发现不足的地方,请不吝批评指教。</div> <div> <br>  本示例的全部源码下载地址:</div> <div> <div>  <a href="http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115" target="_blank"><font color="#0000ff">http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115</font></a> </div> </div> <div> <br>  本示例的运行效果图:<br><div>  <a href="http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115" target="_blank"><font color="#0000ff">http://www.easyjf.com/html/bbs/20060510/1512699849613700.htm?ejid=1141219960424115</font></a> </div>  <br> </div> <br><br><p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=772385</p> <br></ww:>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics