`

Struts搭建完成!(源码)

阅读更多
今天又搭好了我的第一个Struts框架,虽然觉有些复杂。但是我感觉这才有挑战性,下面框架搭建步骤:

******************************struts框架搭建***********************************

使用MyEclipse(实现用户登录)

1.新建Web Project,命名为StrutsProject,J2EE Specification Level为J2EE 1.4,完成。

2.加入Struts(Add Struts Capabilities...),选择Struts1.2,给包命名为cn.mldn.lxh.struts,点击完成

3.新建四个Jsp页面,分别命名为login.jsp,errors.jsp,login_success.jsp,login_failure.jsp,并且标签都为Standard JSP using Struts 1.2
修改代码:
最终四个页面代码为:
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><br>
     <html:submit value="登陆"></html:submit>
     <html:reset value="重置"></html:reset>
   </html:form>
  </body>
</html:html>


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>


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>


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>

4.选择自定义透视图(Custonize Perspective)
选择struts 1.2,建立Struts 1.2 Form,Action&JSP

5.在src下建立Struts 1.2 Form,Action_JSP,
Use case:login
,Superclass:org.apache.struts.action.ActionForm,
添加Form Properties,name和password----->next----->Finish

6.编写代码:

ApplicationResources.properties下代码为:

# Resources for parameter 'cn.mldn.lxh.struts.ApplicationResources'
# Project 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

LoginAction.java下的代码为

* 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: 07-07-2008
 *
 * 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");
  }
  
 }
}

LoginForm.java下的代码为:

* 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: 07-07-2008
 *
 * 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("password",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;
 }
}

7.搭建完成。

施杨出品!!!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics