栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

struts1登录示例代码_动力节点Java学院整理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts1登录示例代码_动力节点Java学院整理

Struts1框架实例—登录实例:

1、实例开始工作—导入jar包,在官网上下载struts1框架包,解压之后导入工程的:

      2、之后配置web.xml(这里的具体配置方法可以参见struts1框架包中的实例文件夹webapps中的实例代码中web.xml文件的配置方法):  

    

     具体如下:     

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

        首先这个配置文件中最主要的就是做了两件的事情,一个是配置ActionServlet,一个是初始化struts-config.xml配置文件参数。 

       3、配置完了web.xml文件,之后我们就要开始进入项目代码阶段了。

       登录页面:      

<%@ page language="java" contentType="text/html; charset=GB18030" 
 pageEncoding="GB18030"%> 
 
 
 
 
Insert title here 
 
 
  
 

       切记那个action后面的路径一定要是.do开头的,因为我们在web.xml中配置的是*.do。这里依旧不介绍为什么随后博客会深入分析。

      4、建立两个异常类,一个是用户名未找到、一个是密码错误:

      ①用户名未找到

public class UserNotFoundException extends RuntimeException { 
 public UserNotFoundException() { 
  // TODO Auto-generated constructor stub 
 } 
 public UserNotFoundException(String message) { 
  super(message); 
  // TODO Auto-generated constructor stub 
 } 
 public UserNotFoundException(Throwable cause) { 
  super(cause); 
  // TODO Auto-generated constructor stub 
 } 
 public UserNotFoundException(String message, Throwable cause) { 
  super(message, cause); 
  // TODO Auto-generated constructor stub 
 } 
}

      ②密码错误 

public class PasswordErrorException extends RuntimeException { 
 public PasswordErrorException() { 
  // TODO Auto-generated constructor stub 
 } 
 public PasswordErrorException(String message) { 
  super(message); 
  // TODO Auto-generated constructor stub 
 } 
 public PasswordErrorException(Throwable cause) { 
  super(cause); 
  // TODO Auto-generated constructor stub 
 } 
 public PasswordErrorException(String message, Throwable cause) { 
  super(message, cause); 
  // TODO Auto-generated constructor stub 
 } 
}

        5、业务处理类代码:

public class UserManager { 
 public void login(String username, String password) { 
  if (!"admin".equals(username)) { 
   throw new UserNotFoundException(); 
  }  
  if (!"admin".equals(password)) { 
   throw new PasswordErrorException(); 
  }   
 } 
}

       6、建立LoginActionForm类,这个类继承ActionForm类,简单说一下这个类,这个类主要是负责收集表单数据的,在这里一定要注意表单的属性必须和actionForm中的get和set方法的属性一致。这里依旧不深入解释,随后博客都会涉及到。       

import org.apache.struts.action.ActionForm; 
 
@SuppressWarnings("serial") 
public class LoginActionForm extends ActionForm {  
 private String username;  
 private String password; 
 public String getUsername() { 
  return username; 
 } 
 public void setUsername(String username) { 
  this.username = username; 
 } 
 public String getPassword() { 
  return password; 
 } 
 public void setPassword(String password) { 
  this.password = password; 
 }  
}

       7、LoginAction类的建立,这个是负责取得表单数据、调用业务逻辑以及返回转向信息。        

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; 
 
public class LoginAction extends Action { 
 @Override 
 public ActionForward execute(ActionMapping mapping, ActionForm form, 
   HttpServletRequest request, HttpServletResponse response) 
   throws Exception { 
  LoginActionForm laf=(LoginActionForm)form; 
  String username=laf.getUsername(); 
  String password=laf.getPassword(); 
  UserManager userManager=new UserManager(); 
  try{ 
   userManager.login(username, password); 
   return mapping.findForward("success"); 
  }catch(UserNotFoundException e){ 
   e.printStackTrace(); 
   request.setAttribute("msg", "用户名不能找到,用户名称=["+username+"]"); 
  }catch(PasswordErrorException e){ 
   e.printStackTrace(); 
   request.setAttribute("msg", "密码错误"); 
  } 
  return mapping.findForward("error"); 
  } 
}

      8、既然有转向,那么我们还要建立两个页面,一个是登录成功页面,一个登录失败页面。

           ①登录成功页面           

<%@ page language="java" contentType="text/html; charset=GB18030" 
 pageEncoding="GB18030"%> 
 
 
 
 
Insert title here 
 
 
 ${loginForm.username },登录成功 
 

           ②登录失败页面            

<%@ page language="java" contentType="text/html; charset=GB18030" 
 pageEncoding="GB18030"%> 
 
 
 
 
Insert title here 
 
 
 <%-- 
 <%=request.getAttribute("msg") %> 
 --%> 
 ${msg } 
 

       9、最后要进行struts-config.xml的配置。
        
  
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"  
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">  
  
      
          
    
  
      
                         type="com.bjpowernode.struts.LoginAction"  
                name="loginForm"          
                scope="request"       
                >  
              
                     
          
      

       经过配置之后实例就已经做完了,感兴趣童鞋可以自己手动运行一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144579.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号