正文

struts初体验之StrutsLoginDemo(2)2008-08-03 10:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/xman/37325.html

分享到:

解析StrutsLoginDemo中的Form, Action And JSP

StrutsLoginDemo中,我们NewForm, Action And JSP后生成了以下文件:

JSP   WebRoot/ userLogin.jsp

Form  src/com.yourcompany.struts.form/UserLoginForm.java

Actionsrc/com.yourcompany.struts.action/UserLoginAction.java

以下解析他们之间的组织关系:

一.Struts配置文件Struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

 

<struts-config>

  <data-sources />

  <form-beans >

    <form-bean name="userLoginForm" type="com.yourcompany.struts.form.UserLoginForm" />

  </form-beans>

 

  <global-exceptions />

  <global-forwards />

  <action-mappings >

    <action

      attribute="userLoginForm"

      input="/userLogin.jsp"

      name="userLoginForm"

      path="/userLogin"

      scope="request"

      type="com.yourcompany.struts.action.UserLoginAction">

      <forward name="success" path="/userLoginSuccess.jsp" />

      <forward name="failure" path="/userLogin.jsp" />

    </action>

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />

</struts-config>

解析:

这个文件指明了Form, Action And JSP三位一体的逻辑关联和组织方法。

1<form-beans >配置ActionForm,至少必须指定两项:
    
name:该项用于标识ActionFormId,以便于传递给Action进行使用
    
type:ActionForm的实现类                             

2<action>配置Action,参数说明:

attributeaction的归属

input—action对应的请求页面
name
bean标签的name对应,是这个action对应的哪一个bean
path—ActionSerlvet
将用户的请求转发与之同名的Action,同名的意思是将请求 ".do"后缀去掉。匹配Actionpath属性值
scope
请求范围,有两种:requestsession
type
指定对应的action

forwardAction的转发映射到实际的jsp页面,在实际编程时应该用逻辑名进行转发
   
一个Action可以有ActionForm,也可以没有ActionForm。如果是post提交的一般会有ActionForm。比如:<form id="frm1" action="loginAction.do">,如果是通过get提交则不会有ActionForm,但是Action总是会有的,因为要处理业务逻辑。比如:<a href="shopAddAction.do">

二.JSP网页文件userLogin.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

    <title>JSP for UserLoginForm form</title>

    </head>

    <body>

    <html:form action="/userLogin">

    userName : <html:text property="userName"/><html:errors property="userName"/><br/>

    password : <html:password property="password"/><html:errors property="password"/><br/>

    <html:submit/><html:cancel/>

    </html:form>

    </body>

</html>

解析:

1<html:form action="/userLogin">action 与上面的path对应就唯一确定了actionform

2userName : <html:text property="userName"/>

   password : <html:password property="password"/>

   分别定义了登录界面userLogin.jsp中的用户text控件及其property、密码password控件及其property

三.ActionForm类实现UserLoginForm.java

package com.yourcompany.struts.form;

import org.apache.struts.action.ActionForm;

 

public class UserLoginForm extends ActionForm {

    /** userName property */

    private String userName;

    /** password property */

    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;

    }

}

解析:

这个类主要定义了两个String类型的变量userNamepassworduserLogin.jsp中的两个

property),并定义了其存取方法(get\set),我们可以类比MFC中的DDX/DDV控件数据校

验机制。

四.Action类实现UserLoginAction.java

package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;

 

public class UserLoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       UserLoginForm userLoginForm = (UserLoginForm) form;// TODO Auto-generated method stub

         if (userLoginForm.getUserName().equals("fan") && userLoginForm.getPassword().equals("20080808"))

            {

                request.setAttribute("userName", userLoginForm.getUserName());

                return mapping.findForward("success");

            }

            else

            {

                return mapping.findForward("failure");

            }

    }

}

解析:

这个类中主要定义了execute方法对数据进行处理。

首先定义类UserLoginForm的实例userLoginForm,然后通过调用其get\set方法来提取数据,并根据数据作出相应处理。

 

参考:

http://www.blogjava.net/happy-java3000/articles/41710.html

http://dev.csdn.net/author/baggio785/ae23a10757e44fd2acd2ebf8cb44ecd8.html

http://java.chinaitlab.com/Struts/521869_3.html

阅读(3045) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册