Struts1 implements simple login (with source code) and struts1 source code
Environment: MyEclipse 14 Release Date: 21:04:55
1. struts1 framework construction
In MyEclipse, create a web project named struts1_login. If this is an empty document, right-click the project and choose myeclipse> add struts capabilities.
Click Install Apache Struts (1.x) Facet above
Click next
Select *. do and change the package name to the one related to your project. For example, my package name is com. lichang. struts1.
Click next
Click Finish, there will be more WEB-INF files under our struts-config.xml
The above is the general process of letting myeclipse help us join the framework. The overall structure of the project is as follows:
So far, the struts1 framework has been set up.
2.
We
Start programming..
Web. xml is as follows:
<?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>
- Then configure the LoginAction code in struts. xml as follows:
<? 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: Specifies the access path type: Specifies the class where the Action is located (generally: package name. class name) name: Specifies the form (similar to the Javabean in jsp). In this example, name corresponds to com. lichang. corresponding to the struts1.LoginActionForm class --> <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-mappings> <message-resources parameter =" com. lichang. struts1.ApplicationResources "/> </struts-config>
The index. jsp code is as follows:<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
- The login_error.jsp code is as follows:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- The login_success.jsp code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
- The LoginAction. java code is as follows:
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; // This class acts as the controller public class LoginAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {// get username and password LoginActionForm laf = (LoginActionForm) form from actionForm; String username = laf. getUsername (); String password = laf. getPassword (); // call the business logic class UserManager userManager = new UserManager (); try {userManager. login (username, password); return mapping. findForward ("success");} catch (UserNotFoundException e) {e. printStackTrace (); request. setAttribute ("msg", "user not found, user name =" + username);} catch (PasswordErrorException e) {e. printStackTrace (); request. setAttribute ("msg", "Incorrect password");} return mapping. findForward ("error ");}}
The LoginActionForm. java code is as follows:Package com. lichang. struts1; import org. apache. struts. action. actionForm;/***** ActionForm (form is used to obtain user input data, which is equivalent to a Javabean in jsp) * However, sturts1 implements some special methods at the underlying layer, so that when Java programmers define Javabean and inherit ActionForm and implement setXXX () method * the values of elements in the user form are assigned to our own Defined variables. For example, the public void setUsername (String username) method can assign username * in form to the username variable **/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 ;}}
The UserManager. java code is as follows:Package com. lichang. struts1; // This class is used to process the user login business logic public class UserManager {public void login (String username, String password) {if (! "Admin". equals (username) {throw new UserNotFoundException ();} if (! "Admin". equals (password) {throw new PasswordErrorException ();}}}
The UserNotFoundException. java code is as follows: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 }}
- The PasswordErrorException. java code is as follows:
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 }}
Running part
- Logon page
The user name does not exist.
Source code: http://files.cnblogs.com/files/li-chang/struts1_login.zip