Struts1登陆Demo

阅读更多

本实例是Struts1的一个入门实例

采用的编辑器是Myeclipse 10

 

一,创建模型

本事列涉及两个JavaBean

1.UserInfo.java:描述用户的实体类

package cx.mystruts.entity;

public class UserInfo {
	private String uid;
	private String pwd;
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public UserInfo(String uid, String pwd) {
		super();
		this.uid = uid;
		this.pwd = pwd;
	}
	public UserInfo() {}

}

2.UserService.java:处理业务逻辑的JavaBean

package cx.mystruts.service;

import java.util.HashMap;
import java.util.Map;

import cx.mystruts.entity.UserInfo;

public class UserService {
	
	//构造Map对象,用于存入UserInfo的集合
	private Map userInfoMap=new HashMap();
	//构造UserService静态对象,实现单列工厂模式
	private static UserService userService=new UserService();
	
	//构造函数私有,单列模式,模拟数据库产生UserInfo的集合
	private UserService(){
		try{
			UserInfo u=new UserInfo("cx","123456");
			userInfoMap.put(u.getUid(), u);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//定义静态方法
	public static UserService newInstance(){
		return userService;
	}
	
	public UserInfo login(String uid,String pwd){
		//判断用户输入的用户名是否在集合中
		if(this.userInfoMap.containsKey(uid)){
			UserInfo findUserInfo=(UserInfo)this.userInfoMap.get(uid);
			if(findUserInfo.getPwd().equals(pwd)){
				return findUserInfo;
			}
			return null;
		}
		return null;
	}

}

 二.以下步骤需要添加Struts支持,直接使用MyEclipse添加,这里不介绍

1.创建ActionForm

package cx.struts;

import org.apache.struts.action.ActionForm;

public class LoginActionForm extends ActionForm {
	
	private String uid;
	private String pwd;
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	

}

 2.创建控制器

   (1)ActionMapping:包含Action的配置信息,与Struts-config.xml配置文件中的元素对应。

   (2)ActionForm:包含用户的表单数据。当Struts1框架调用execute()方法时,ActionFrom中的数据已经通过了表单验证。

   (3)HttpServletRequest:当前的HTTP请求对象

   (4)HttpServletResponse:当前的HTTP响应对象

package cx.struts;

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 cx.mystruts.entity.UserInfo;
import cx.mystruts.service.UserService;

public class LoginAction extends Action{
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
		LoginActionForm frm=(LoginActionForm)form;
		String uid=frm.getUid();
		String pwd=frm.getPwd();
		
		UserInfo user=UserService.newInstance().login(uid, pwd);
		if(user!=null){
			request.setAttribute("user", user);
			return mapping.findForward("success");
		}
		return mapping.findForward("fail");
	}

}

 三.视图

1.登陆界面index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  

    
    User Login

  
  
  
    
UID: PWD:

 2.登陆成功界面success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  
    Success
  
  
  
  

Login Success

UID:${user.uid }
PWD:${user.pwd }

 3.登陆失败界面fail.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  
    
    fail
    
  
  
  
    

Login Fail

 四.Struts配置文件

1.webxml



  
  
    action
    org.apache.struts.action.ActionServlet
    
      config
      /WEB-INF/struts-config.xml
    
    
      debug
      3
    
    
      detail
      3
    
    0
  
  
    action
    *.do
  
  
    index.jsp
  

 2.Struts-config.xml





  
  
    
  
  
  
  
     
      
      
       
      
    
     
  
  

 

 

OK!到此,整个登陆流程就Ok!

你可能感兴趣的:(struts1,login,demo)