Struts入门

系统 2308 0
[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>




Struts入门


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论