随笔 - 303  文章 - 883  trackbacks - 0
<2008年2月>
272829303112
3456789
10111213141516
17181920212223
2425262728291
2345678

欢迎光临! 
闲聊 QQ:1074961813

随笔分类(357)

我管理的群

公共blog

  • n维空间
  • Email : java3d@126.com 群 : 12999758

参与管理的论坛

好友的blog

我的其他blog

朋友的网站

搜索

  •  

最新评论

正如很多J2ee一样,这个程序功能非常简单就是实现登陆验证,代码也十分简单,但是,编写的时候经常出错,原因是一些小地方没注意到。这里提供程序的下载并,提供部分重点容忽视代码的解释。

如何让spring和struts融合在一起?写代码过程中一篇来自IBM的文章给了我很大的帮助,下面对我的这个程序做简要说明

该步骤可以在两个地方实现
第一,每个web工程都有一个web.xml配置文件,这个文件在Tomcat启动的时候会被Tomcat加载,和很多配置文件的原理一样,这个文件为Tomcat和Tomcat建立了一个通讯渠道,所以,我们需要在里面做些配置,通过它去告诉Tomcat服务器,我们这个web工程里用了struts和spring,具体的方法是:
告诉它我们用了stucts

    <servlet>
        
<servlet-name>action</servlet-name>
        
<servlet-class>
            org.apache.struts.action.ActionServlet
        
</servlet-class>
        
<init-param>
            
<param-name>config</param-name>
            
<param-value>/WEB-INF/struts-config.xml</param-value>
        
</init-param>
        
<init-param>
            
<param-name>debug</param-name>
            
<param-value>3</param-value>
        
</init-param>
        
<init-param>
            
<param-name>detail</param-name>
            
<param-value>3</param-value>
        
</init-param>
        
<load-on-startup>0</load-on-startup>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>action</servlet-name>
        
<url-pattern>*.lusm</url-pattern>
    
</servlet-mapping>

和它说我们用了spring

    <servlet>
        
<servlet-name>context</servlet-name>
        
<servlet-class>
            org.springframework.web.context.ContextLoaderServlet
        
</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>/WEB-INF/applicationContext.xml</param-value>
    
</context-param>
其实就是把struts和spring对应的两个xml配置文件,注入到里面,这样web容器就知道我们用了struts和spring啦!!!

第二、在代码中整合srping和struts。
我们记得在写struts的****Action的时候,这个Action通常继承自org.apache.struts.action.Action ,但现在情况不同啦!!!正所谓识时务者为英雄,既然我们要使用sping了,就不可墨守成规,实际使用中我们让Action继承org.springframework.web.struts.ActionSupport,注意这对我们后面的,通过spring的ApplicationContext con =  getWebApplicationContext(); 起了关键作用,通过它我们可以让spring通过web.xml获取/WEB-INF/applicationContext.xml获取配置信息,从而获取一个通过spring实例化的bean---user,接着,我们拿出struts中实例化的另一个实例(通过我们提交的数据,实例化的bean)userForm下面是代码:(注意其中的new String(  )方法)

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

package com.lusm.struts.action;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.lusm.struts.form.UserForm;

import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;

public class UserLoginAction extends ActionSupport{
    
/*
     * Generated Methods
     
*/

    
//这个是必然会执行的
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        ApplicationContext con 
=  getWebApplicationContext(); 
        UserForm userForm 
= (UserForm) form;
        
        UserForm user 
= (UserForm)con.getBean("User");
        
        
if(new String(userForm.getName()).equals(new String(user.getName()))
                
&&
           
new String(userForm.getPasswd()).equals(new String(user.getPasswd())))
        
{
            System.out.println(
"验证成功");
            
return mapping.findForward("success");
        }

        
else{
            System.out.println(
"验证失败");
            
return mapping.findForward("error");
        }

    }

}

运行结果:



其中的jar太大了请自己去官方下载:


程序下载:
   __download__




地震让大伙知道:居安思危,才是生存之道。
posted on 2008-02-26 18:33 小寻 阅读(1072) 评论(1)  编辑  收藏 所属分类: j2se/j2ee/j2me

FeedBack:
# re: Structs && Spring 登陆实验 2008-03-22 18:02 幻想~@@~

如何在servlet使用session,以上面的代码为例
UserLoginAction.java里做如下更改:

 

//>>>>>>>>>>部分代码>>>>>>>>>>>>>>>>>
        HttpSession session = request.getSession(true);
        response.setContentType("text/html");
        
        UserForm userForm 
= (UserForm) form;
        
        UserForm user 
= (UserForm)con.getBean("User");
        
        
if(new String(userForm.getName()).equals(new String(user.getName()))
                
&&
           
new String(userForm.getPasswd()).equals(new String(user.getPasswd())))
        
{
            session.setAttribute(" username ",user.getName());
            System.out.println("验证成功");
            
return mapping.findForward("success");
        }

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


在所有jsp或servlet页面都可以使用

${username}//将username的值取出来,这里是"lusm"
  回复  更多评论
  

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


网站导航: