[b][size=18]采用的是MYECLIPSE5.1 该登录并没有采用连接数据库.共分为四个页面
1.login.jsp 登录表单2.errors.jsp打印错误信息 3.login_success.jsp 登录成功页面4.login_failure.jsp 登录失败页面
开发前首先加入STRUTS支持.
第一步:
1:首先新建一个登录页面(login.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>用户登陆</h2>
<html:form action="login.do" method="post">
用户名:<html:text property="name"></html:text><br>
密码:<html:password property="password"></html:password>
<html:submit value="登陆"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
</body>
</html:html>
2:登录成功页面(login_success.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆成功!!!</h1>
</body>
</html:html>
3:登录失败页面(login_failure.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆成功!!!</h1>
</body>
</html:html>
4:错误页面(errors.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>登陆时发生了以下错误:</h3>
<html:errors/>
</body>
</html:html>
第二步:
新建ACTIONFORM 和ACTION
1:LoginForm如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.mldn.lxh.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* MyEclipse Struts
* Creation date: 12-12-2006
*
* XDoclet definition:
* @struts.form name="loginForm"
*/
public class LoginForm extends ActionForm {
/*
* Generated fields
*/
/** password property */
private String password;
/** name property */
private String name;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors = new ActionErrors() ;
if(this.name==null||"".equals(this.name))
{
errors.add("name",new ActionMessage("name.null")) ;
}
if(this.password==null||"".equals(this.password))
{
errors.add("name",new ActionMessage("password.null")) ;
}
return errors;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}
/**
* Set the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}
2:LoginAction如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.mldn.lxh.struts.action;
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 cn.mldn.lxh.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 12-12-2006
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
String name = loginForm.getName() ;
String password = loginForm.getPassword() ;
if("mldn".equals(name)&&"lxh".equals(password))
{
// 跳转到成功页
return mapping.findForward("suc") ;
}
else
{
// 跳转到失败页
return mapping.findForward("fal") ;
}
}
}
3:资源文件如下:ApplicationResources.properties
# Resources for parameter 'cn.mldn.lxh.struts.ApplicationResources'
# Project P/StrutsProject
name.null = <li>\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
password.null = <li>\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
4:配置文件如下 Struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="cn.mldn.lxh.struts.form.LoginForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/errors.jsp"
name="loginForm"
path="/login"
scope="request"
type="cn.mldn.lxh.struts.action.LoginAction">
<forward name="suc" path="/login_success.jsp"></forward>
<forward name="fal" path="/login_failure.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="cn.mldn.lxh.struts.ApplicationResources" />
</struts-config>
1.login.jsp 登录表单2.errors.jsp打印错误信息 3.login_success.jsp 登录成功页面4.login_failure.jsp 登录失败页面
开发前首先加入STRUTS支持.
第一步:
1:首先新建一个登录页面(login.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>用户登陆</h2>
<html:form action="login.do" method="post">
用户名:<html:text property="name"></html:text><br>
密码:<html:password property="password"></html:password>
<html:submit value="登陆"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
</body>
</html:html>
2:登录成功页面(login_success.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆成功!!!</h1>
</body>
</html:html>
3:登录失败页面(login_failure.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆成功!!!</h1>
</body>
</html:html>
4:错误页面(errors.jsp)
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>login.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>登陆时发生了以下错误:</h3>
<html:errors/>
</body>
</html:html>
第二步:
新建ACTIONFORM 和ACTION
1:LoginForm如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.mldn.lxh.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* MyEclipse Struts
* Creation date: 12-12-2006
*
* XDoclet definition:
* @struts.form name="loginForm"
*/
public class LoginForm extends ActionForm {
/*
* Generated fields
*/
/** password property */
private String password;
/** name property */
private String name;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
ActionErrors errors = new ActionErrors() ;
if(this.name==null||"".equals(this.name))
{
errors.add("name",new ActionMessage("name.null")) ;
}
if(this.password==null||"".equals(this.password))
{
errors.add("name",new ActionMessage("password.null")) ;
}
return errors;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}
/**
* Set the password.
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}
2:LoginAction如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.mldn.lxh.struts.action;
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 cn.mldn.lxh.struts.form.LoginForm;
/**
* MyEclipse Struts
* Creation date: 12-12-2006
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
*/
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
String name = loginForm.getName() ;
String password = loginForm.getPassword() ;
if("mldn".equals(name)&&"lxh".equals(password))
{
// 跳转到成功页
return mapping.findForward("suc") ;
}
else
{
// 跳转到失败页
return mapping.findForward("fal") ;
}
}
}
3:资源文件如下:ApplicationResources.properties
# Resources for parameter 'cn.mldn.lxh.struts.ApplicationResources'
# Project P/StrutsProject
name.null = <li>\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
password.null = <li>\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
4:配置文件如下 Struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="loginForm" type="cn.mldn.lxh.struts.form.LoginForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/errors.jsp"
name="loginForm"
path="/login"
scope="request"
type="cn.mldn.lxh.struts.action.LoginAction">
<forward name="suc" path="/login_success.jsp"></forward>
<forward name="fal" path="/login_failure.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="cn.mldn.lxh.struts.ApplicationResources" />
</struts-config>