和时间赛跑
我要追上时间
和时间赛跑的人!
我的首页
文章
相册
圈子
留言
管理
 
    当前所在页面:首页>>文章>>一个EasyJWeb整合Spring的例子
一个EasyJWeb整合Spring的例子
    作者:erikchang 来源: 发表时间:2008-01-01

 
 
Spring得以广泛应用的主要就是他的IOC以及AOP支持,给编程过程中带来了非常大的方便,EasyJWeb在使用中也不例外,虽然EasyJWeb有自己的一套IOC但是和Spring相比还是有很大的差别,不过EasyJWeb提供了一个内置容器来兼容Spring,通过这个内置容器EasyJWeb就可以给Bean的管理完全交给Spring,这样就可以使用Spring的所有特性,达到程序简易化的目的。
在进行Spring整合EasyJWeb前给以下jar文件添加到classpath中:
easyjweb-core-1.0-m3.jar
easyjweb-ext-1.0-m3.jar
       dom4j-1.6.1.jar
easydbo-0.9.1.jar
velocity-1.5.jar
velocity-dep-1.5.jar
jaxen-1.1.jar
log4j-1.2.14.jar
commons-fileupload-1.2.jar
commons-logging-1.1.jar
首先定义一个实体类User,代码如下:
package spring.chapter6.easyjweb.domain;
publicclass User {
    private String userName;
    private String passWord;
    private Sex sex;
    public String getUserName() {
       returnuserName;
    }
 
    publicvoid setUserName(String userName) {
       this.userName = userName;
    }
 
    public String getPassWord() {
       returnpassWord;
    }
 
    publicvoid setPassWord(String passWord) {
       this.passWord = passWord;
    }
 
    public Sex getSex() {
       returnsex;
    }
 
    publicvoid setSex(Sex sex) {
       this.sex = sex;
    }
}
User中有3个属性,用户名userName、密码passWord、性别sex,其中性别是一个新的实体,代码如下:
package spring.chapter6.easyjweb.domain;
publicclass Sex {
    private String sex;
 
    public String getSex() {
       returnsex;
    }
 
    publicvoid setSex(String sex) {
       this.sex = sex;
    }
}
性别类可以存在数据库中,这里直接从配置文件中注入。
编写UserAction,该action继承AbstractCmdAction,代码如下:
package spring.chapter6.easyjweb.action;
import spring.chapter6.easyjweb.domain.User;
import com.easyjf.container.annonation.Inject;
import com.easyjf.web.Module;
import com.easyjf.web.Page;
import com.easyjf.web.WebForm;
import com.easyjf.web.core.AbstractCmdAction;
 
publicclass UserAction extends AbstractCmdAction {
    @Inject
    private User user;
 
    publicvoid setUser(User user) {
       this.user = user;
    }
 
    public Page doInit(WebForm form, Module module) {
       returnnew Page("/index.html");
    }
 
    public Page doShow(WebForm form, Module module) {
       form.addResult("user", form.toPo(user));
       returnnew Page("/show.html");
    }
}
action中有2个方法,初始化方法doInit()直接到开index.html页面,表示当请求为“user.ejf”时候打开index.html页面,doShow()方法封装表单对象到WebForm中,并导向新的页面show.html,这样就可以在show.html中使用velocity标签来显示数据了,这里@Inject标签是easyjweb自身的标签,easyjweb将会自动从配置文件中来获取相同类型的Bean并注入到UserAction中。
web配置文件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">
    <context-param>
       <param-name>easyjwebConfigLocationparam-name>
       <param-value>/WEB-INF/mvc.xmlparam-value>
    context-param>
   
    <servlet>
       <servlet-name>easyjfservlet-name>
       <servlet-class>com.easyjf.web.ActionServletservlet-class>
       <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
       <servlet-name>easyjfservlet-name>
       <url-pattern>*.ejfurl-pattern>
    servlet-mapping>
   
    <filter>
       <filter-name>CharsetFilterfilter-name>
       <filter-class>com.easyjf.web.CharsetFilterfilter-class>
       <init-param>
           <param-name>encodingparam-name>
           <param-value>UTF-8param-value>
       init-param>
       <init-param>
           <param-name>ignoreparam-name>
           <param-value>trueparam-value>
       init-param>
    filter>
    <filter-mapping>
       <filter-name>CharsetFilterfilter-name>
       <servlet-name>easyjfservlet-name>
    filter-mapping>
web-app>
配置文件中首先指定了easyjwebConfigLocation来获取easyjweb配置文件,这里指定easyjweb配置文件为WEB-INF/mvc.xml,然后指定所有“.ejf”后缀的请求均交给ActionServlet来处理,最后还是指定了一个Filter来统一编码。
接着编写easyjweb的配置文件,easyjweb的配置文件只用来配置action,其它所有的Bean都交给spring来管理,mvc.xml代码如下:
xml version="1.0" encoding="UTF-8"?>
<easyjf-web xmlns="http://www.easyjf.com/schema/easyjf/web"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.easyjf.com/schema/easyjf/web http://www.easyjf.com/schema/easyjf/web/easyjf-web-0.0.1.xsd">
    <beans>
      
       <bean name="springContainer"
           class="org.springframework.web.context.support.XmlWebApplicationContext">
           <property name="configLocations">
              <list>
                  <value>WEB-INF/bean-config.xmlvalue>
              list>
           property>
       bean>
       <bean name="innerSpringContainer"
           class="com.easyjf.container.impl.SpringContainer">
           <property name="factory" ref="springContainer" />
       bean>
      
    beans>
    <modules>
       <module name="user" path="/user"
           action="spring.chapter6.easyjweb.action.UserAction">
       module>
    modules>
easyjf-web
配置文件中首先使用Spring的XmlWebApplicationContext来读取Spring的Bean配置文件,支持多个文件导入,配置文件中的bean-config.xml就是spring的配置文件,接着使用easyjweb的SpringContainer来引入XmlWebApplicationContext,这样就可以在easyjweb中任意使用Spring配置的Bean,@Inject标签就会从Spring的配置中来查找Bean并注入到action中。在中只配置了一个,每一个对应一个action,这里只有UserAction,表示当请求为“user.ejf”的时候servlet将会转交给UserAction来处理。
编写Spring配置文件bean-config.xml代码如下:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="user" class="spring.chapter6.easyjweb.domain.User">
       <property name="sex" ref="sex" />
    bean>
    <bean id="sex" class="spring.chapter6.easyjweb.domain.Sex">
       <property name="sex" value="" />
    bean>
    <bean id="userAction"
       class="spring.chapter6.easyjweb.action.UserAction">
       <property name="user" ref="user" />
    bean>
beans>
该配置文件中配置了User以及Sex类,这里直接给Sex注入了一个性别“男”,并且给Sex注入到User类中,这样结合easyjweb的@Inject标签就就可以自动将User注入到Action中。
 编写页面文件index.html、show.html,页面代码都使用velocity作为模板引擎,具体代码如下:
index.html
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>Spring整合EasyJWeb实例title>
head>
<body>
<form action="/SpringEasyJWeb/user.ejf?cmd=show" method="post">用户名:<input
    type="text" name="userName" value="" /><br>
密码:<input type="password" name="passWord" value="" /><br>
<input type="submit" value="提交" />form>
body>
html>
index.html中字段属性名也和User实体中的一样,这样就可以使用form.toPo()来将表单转换成一个User实体。
show.html
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>Spring整合EasyJWeb实例title>
head>
<body>
$!{user.userName}您好!您的密码是:$!{passWord},性别是:$!{user.sex.sex}
body>
html>

index.html以及show.html均要放到WEB-INF/views/下。

 
 

(阅读 )   评论数(:1)
评论】 【收藏】
评论:共1条
写的不错,但是代码没发好,编辑器里提供了代码框功能的。
评论人: 天一     评论时间: 2008-01-04 09:37:42

发表评论:
发表人:
评论: 
    
 
关于我们 | 诚聘英才 | 联系我们 | 广告业务 | 网站地图 | 法律声明

EasyJF开源团队版权所有  建议使用1024*768分辨率