Struts+Spring+Hibernate整合的例子(四)

 下面我们开始创建Struts的程序,我们在WEB-INF下双击打开struts-config.xml文件,在视图中的空白处右键--New--"Form,Action and JSP":

弹出一对话框,如下图:

此步是创建Form,在User case中输入user,在下面添加Form的属性(点"Add"按钮添加),然后我们我们选择“JSP”选项卡,如下图:

按图中配置即可,创建的jsp文件会出现在WebRoot/jsp文件夹中(应该在WebRoot下创建个jsp文件夹),

然后点击"Next"进入下一步,如下图:

此步是创建Action,在上面输入Path,因为是登陆,这里我们输入"/login",然后确定Type中是com.zgh.struts.action.LoginAction然后点击“Forwards”选项卡,如下图:

按图中的步骤一步一步输入(或选择),先点击“Add” (第2步)在弹出框中的“Name”中输入“success”(第4步),然后点"Browser"按钮(第5步),选择一个登陆成的的页面(第6步,这个页面应该先建好,后面会给出代码),然后点OK(第7步),然后点"Add"按钮(第8步),然后点“Close”关闭(第9步),然后点"Finish"完成(第10步)。完成。在struts-config.xml中生成视图如下:

以下是struts-config.xml的代码,如下:

 

<? xml version="1.0" encoding="gbk" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >

< struts-config >
  
< data-sources  />
  
< form-beans  >
    
< form-bean  name ="userForm"  type ="com.zgh.struts.form.UserForm"   />

  
</ form-beans >

  
< global-exceptions  />
  
< global-forwards  />
  
< action-mappings  >
   
<!--  把type="com.zgh.struts.action.LoginAction" 改成:type="org.springframework.web.struts.DelegatingActionProxy"   -->
   
<!--  这样才能合Spring整合,把控制权交给Spring  -->
    
< action
      
attribute ="userForm"
      input
="/jsp/login.jsp"
      name
="userForm"
      path
="/login"
      scope
="request"
     
      type
="org.springframework.web.struts.DelegatingActionProxy" >
      
< forward  name ="success"  path ="/jsp/loginSuccess.jsp"   />
    
</ action >

  
</ action-mappings >

  
< message-resources  parameter ="com.zgh.struts.ApplicationResources"   />
  
<!--  添加Spring的插件  -->
 
< plug-in  className ="org.springframework.web.struts.ContextLoaderPlugIn" >
      
< set-property  property ="contextConfigLocation"  value ="/WEB-INF/applicationContext.xml" />
  
</ plug-in >
  
</ struts-config >

注意下上面的action元素的type属性,这是与Spring整合的一种方法,还有后面的plug-in是必须的,而且一定要写到最后,Struts的配置文件必须按先后顺序。

下面是LoginAction.java代码:

 

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

/**
 * 
@author zhu国辉
 
*/

package  com.zgh.struts.action;

import  java.util.List;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

import  com.zgh.spring.service.IUserLoginService;
import  com.zgh.struts.form.UserForm;

/** 
 * MyEclipse Struts
 * Creation date: 01-07-2008
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="userForm" input="/jsp/login.jsp" scope="request" validate="true"
 * @struts.action-forward name="success" path="/jsp/loginSuccess.jsp"
 
*/

public   class  LoginAction  extends  Action  {
    
/*
     * Generated Methods
     
*/


    
private IUserLoginService userLoginService;
    
    
public void setUserLoginService(IUserLoginService userLoginService) {
        
this.userLoginService = userLoginService;
    }


    
/** 
     * Method execute
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     
*/

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        UserForm userForm 
= (UserForm) form;// TODO Auto-generated method stub
        List users=userLoginService.getUser(userForm.getUsername(), userForm.getPassword());
        
if(users!=null && users.size()>0){
            
return mapping.findForward("success");
        }

        
return mapping.getInputForward();
    }

}

下面是login.jsp的代码:

 

<% @ page language="java" %>
<% @ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>  
<% @ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
 
< html >  
    
< head >
        
< title > JSP for UserForm form </ title >
    
</ head >
    
< body >
        
< html:form  action ="/login" >
            
            username : 
< html:text  property ="username" />< html:errors  property ="username" />< br />
            password : 
< html:password  property ="password" />< html:errors  property ="password" />< br />
            
< html:submit />< html:cancel />
        
</ html:form >
    
</ body >
</ html >

以下是loginSuccess.jsp代码:

 

<% @ page language="java" contentType="text/html; charset=GBK"
    pageEncoding
="GBK"
%>
<% @ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>  
<% @ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
    
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GBK" >
< title > Insert title here </ title >
</ head >
< body >
    亲爱的
< bean:write  name ="userForm"  property ="username" /> :
    
< br />
    欢迎您!
</ body >
</ html >
好,接下来的在下一页继续。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值