struts实现登录 struts1实现简单的登录功能实例(附源码)
sizai 人气:0想了解struts1实现简单的登录功能实例(附源码)的相关内容吗,sizai在本文为您仔细讲解struts实现登录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:struts实现登录,struts2,登录功能,用struts2实现登录,下面大家一起来学习吧。
环境:MyEclipse 14
1 struts1 框架搭建
在MyEclipse新建web project 取名为struts1_login,此时是一个空文档就不截图了然后在project上右键->选择myeclipse->add struts capabilities
单击上面Install Apache Struts(1.x)Facet
点击next
选择*.do ,改下包名改成与你项目相关的。如我的包名为com.lichang.struts1
点击next
点击完成,在我们的WEB-INF下就会多出struts-config.xml文件
以上就是让myeclipse帮我们加入框架的大概过程。项目的整体结构如下:
至此我们的struts1 框架搭建完成2 接着我们就开始编程来实现了。
2 接着我们就开始编程来实现了。
web.xml 如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts1_login</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>*.do</url-pattern> </servlet-mapping> </web-app>
然后在struts.xml配置LoginAction 代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.lichang.struts1.LoginActionForm"/> </form-beans> <action-mappings> <!-- path:指定访问时的路径 type:指定Action所在的类(一般是:包名.类名) name:指定和哪个表单(和jsp中Javabean 差不多的东西)对应,该例中name就和com.lichang.struts1.LoginActionForm类对应--> <action path="/login" type="com.lichang.struts1.LoginAction" name="loginForm" scope="request" > <forward name="success" path="/login_success.jsp" /> <forward name="error" path="/login_error.jsp"/> </action> </action-mappings> <message-resources parameter="com.lichang.struts1.ApplicationResources" /> </struts-config>
index.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html style="text-align:center"> <head style="text-align:center"> <title>index page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body style="text-align:center"> <form action="login.do" method="post"> 用户:<input type="text" name="username"><br> 密码:<input type="password" name="password"></br> <input type="submit" value="登录"> </form> </body> </html>
login_error.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>error page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html>
login_success.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>success page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> ${username },登录成功 </body> </html>
LoginAction.java代码如下:
package com.lichang.struts1; 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 { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //从actionForm中获取username和password 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"); } }
LoginActionForm.java代码如下:
package com.lichang.struts1; import org.apache.struts.action.ActionForm; /** * * ActionForm(表单用于获取用户输入的数据,相当于jsp中的Javabean) * 不过sturts1在底层上实现了一些特别的方法以至于当Java程序员定义了Javabean并继承ActionForm并实现setXXX()方法时 * 用户表单中元素的值就被一一赋给我们自己定义的变量。如public void setUsername(String username)方法就可把form中username * 赋给username变量 * */ 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; } }
UserManager.java代码如下:
package com.lichang.struts1; //这个类就是用来处理用户登录的业务逻辑 public class UserManager { public void login(String username, String password) { if (!"admin".equals(username)) { throw new UserNotFoundException(); } if (!"admin".equals(password)) { throw new PasswordErrorException(); } } }
UserNotFoundException.java代码如下:
package com.lichang.struts1; 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 } }
PasswordErrorException.java代码如下:
package com.lichang.struts1; 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 } }
运行部分截图
登录界面
用户名不存在
源代码下载地址: struts1_login_jb51.rar
加载全部内容