posts - 119, comments - 62, trackbacks - 0, articles - 0
开发环境:
window xp
jdk 1.5
tomcat 5.028
eclispe 3.2
myeclipse 4.0
步骤:
(1)新建一个web project,名称为suzhouadmin
(2)在项目上点鼠标右键,选择myeclipse->add spring capablities...
(3)把Spring 1.2 AOP,Spring 1.2 Core Lib,Spring ORM/DAO/Hibernate 3.0 lib,Spring 1.2 web lib选择中,Copy Checked lib content to project folder,然后点Next
(4)folder改为:WebRoot/WEB-INF,File为applicationContext.xml不变。点Finish。
Spring的引用完成。

(5)在项目上点鼠标右键,选择myeclipse->add Struts capablities...
(6)选择struts1.1,base package for classes 改成 com.suzhou.admin.struts,然后点finish。

Struts的引用完成。
(6)新建立一个包com.suzhou.admin.hibernate,然后在项目上点鼠标右键,选择myeclipse->add Hibernate capablities...
(7)把Hibernate 3.0 core lib,Hibernate 3.0 advanced lib中,Copy Checked lib content to project folder,点Next
(8)选择Spring Configuration file(applicationContext.xml),点Next
(9)选择Exit spring configuration file.输入sessionFactory id为sessionFactory。点Next
(10)设置数据源ID为dataSource,选择一个已经设置好的DB profile,然后点Next
(11)点Package...选择com.suzhou.admin.hibernate,自动生成类名com.suzhou.admin.hibernate.HibernateSessionFactory。

开始配置Struts和Spring.
(12)打开WEB-INFO/web.xml,在最后面添加:
 <servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
这样,Spring 的ApplicationContext就配置好了。通过以上配置,Web容器会自动加载/WEB-INF/applicationContext.xml初始化
ApplicationContext实例,如果需要指定配置文件位置,可通过context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
配置完成之后,即可通过
WebApplicationContextUtils.getWebApplicationContext()
方法在Web应用中获取ApplicationContext引用。

(13)通过Struts config新建一个Action名为LoginAction,自动生成的action定义如下:
    <action
      attribute="loginForm"
      input="/index.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="com.suzhou.admin.struts.action.LoginAction">
      <forward name="loginfail" path="/error.jsp" />
      <forward name="loginok" path="/main.jsp" />
    </action>
把其改成:
    <action
      attribute="loginForm"
      input="/index.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="loginfail" path="/error.jsp" />
      <forward name="loginok" path="/main.jsp" />
    </action>
并在struts-config.xml中添加Spring插件。
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
 </plug-in>
(13)用Myeclipse Spring donfig editor打开applicationContext.xml,点右键,选择new Bean,输入Bean name为/login,calss为com.suzhou.admin.struts.action.LoginAction。点finish,applicationContext.xml多加了一个Bean定义:
<bean name="/login" class="com.suzhou.admin.struts.action.LoginAction" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"></bean>

只有注册用户登录后才能发表评论。


网站导航: