zoukankan      html  css  js  c++  java
  • Struts详细用法

    Structs(一)

    1.首先在Project里右键Myeclipse,选择add structs capabilities.

    Structs-config.xml核心配置文件,名字可以改动。

    之后在src下多了一个类包,里面有一个资源文件。多了structs和依赖包,多了标签和xml配置文件。

    //PS:(个人理解:action可以改,而且随着配制的改变,xml里面也会相应的改动,当jsp提交到.do文件时,会找到action这个servlet,然后再去找form..java和action.java)。

    注意在web.xml里面的变化

    <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>

      <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

      </welcome-file-list>

    </web-app>

    2.新建一个jsp

    Taglib标签:

    url是唯一标识符,在导入标签里面有一个structs-bean.tld里面有一句话

    <uri>http://struts.apache.org/tags-bean</uri>是对应的。

    后面prefix是你应用标签时需要写的关键字。

    3.开发完页面之后,不需要创建servlet,需要创建一个代替他的东西。

    在你src新添加的类包中,点击new-other下面的myeclipse->web-Structs->1.2->structs1.2 form,action &jsp.

    Form是管前半部分的,用于接收参数,而action是管后半部分的用于验证,跳转等功能。

    配置Form

    其中NAME那个选项:

    Jsp里面提交到login.do,这里就把do前面那个填进去。

    配置Action

    注意:最后Input Source是输入页面。

    发现在类包中多了Form和Action包。

    4.先进入到form包中。

    在LoginForm.java中多了一个方法

        public ActionErrors validate(ActionMapping mapping,

               HttpServletRequest request) {

           ActionErrors errors=new ActionErrors();

           if(this.username==null||this.username.trim().equals(""))

           {

               errors.add("username", new ActionMessage("username.null"));

           }

           if(this.password==null||this.password.trim().equals(""))

           {

               errors.add("password" , new ActionMessage("password.null"));

           }

           return errors;

        }//用来完成验证信息,一般用来验证非空和数据格式。

    5.src下新生成的ApplicationResources.properties这个文件下配置错误信息。

    username.null=/u7528/u6237/u540d/u4e0d/u80fd/u4e3a/u7a7a/uff01

    password.null=/u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a/uff01

    (第一个编码为:用户名不能为空!,第二个为:密码不能为空!因为不识别汉字,所以需要编码,编码转换过程如下1,先将所需转换文字复制,然后找到你的Java/jdk1.6.0_10/bin目录,然后找到native2ascii这个文件,2,打开3,粘贴,回车即得编码)

    6.login.jsp这个页面里的表单中加入错误提示信息。

     

    <table border="0">

            <tr>

              <td>用户名:</td>

              <td><html:text property="username" /></td>

              <td><html:errors property="username"/></td>

            </tr>

            <tr>

              <td>密码:</td>

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

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

            </tr>

            <tr>

              <td colspan="2" align="center"><html:submit value="提交" /></td>

            </tr>

          </table>

    7.再进入到action包中,在LogonAction.java中修改execute方法。

     

    public ActionForward execute(ActionMapping mapping, ActionForm form,

                         HttpServletRequest request, HttpServletResponse response) {

                  LoginForm loginForm = (LoginForm) form;       if(loginForm.getUsername().equals("robin")&&loginForm.getPassword().equals("123"))

                  {

                         request.getSession().setAttribute("uname", loginForm.getUsername());

                         //跳转是通过返回值跳转。

                         return mapping.findForward("suc");

                  }

                  else {

                         ActionErrors errors=new ActionErrors();

                         errors.add("loginerror", new ActionMessage("login.error"));

                         this.addErrors(request, errors);

                         return mapping.getInputForward();

                  }

           }

     

    8.ApplicationResources.properties文件下加入错误信息

    login.error=/u7528/u6237/u540d/u6216/u8005/u5bc6/u7801/u9519/u8bef/uff01

    9.新建跳转页suc.jsp

    <%@page import="java.util.*" %>

    <%@ page contentType="text/html;charset=GBK"%>

    <html>

    <head>

    <title>success</title>

    </head>

    <body>

    ${uname } 登陆成功!

    </body>

    </html>

    10.配置跳转路径,在struts-config.xml中加入路径配置

    <action-mappings >

        <action

          attribute="loginForm"

          input="/jsp/login.jsp"

          name="loginForm"

          path="/login"

          scope="request"

          type="com.proper.struts.action.LoginAction">

          

          <set-property property="cancellable" value="true" />

          <forward name="suc" path="/jsp/suc.jsp"/>   

        </action>

      </action-mappings>

    11Login.jsp页面中加入显示错误信息

      <body>

      <center>

        <font color="red"><html:errors property="loginerror"/></font>

        <html:form action="login.do">

          <table border="0">

            <tr>

              <td>用户名:</td>

              <td><html:text property="username" /></td>

              <td><html:errors property="username"/></td>

            </tr>

    。。。

        

    显示页面如下:

    -----------------------------------工作原理与核心配置

    在使用Jsp/Servlet时的原理为

    Jsp跳到web.xml中的servlet和servlet-mapping,找到对应的servlet类来接收参数,验证,调用dao等操作。再跳转到其他页面。

    使用structs时的原理为

    Jsp->web.xml(通过url-pattern: *.do)-> ActionServlet->structs.cfg.xml

    (根据path路径找到对应的actionForm和action)->首先是ActionFrom->validate发放验证(正确跳到action,否则跳回input错误页)->action->调用DAO进行逻辑判断->成功时跳转某个页(structs.cfg.xml:forward),失败时返回某个错误页面(input).

    Structs.cfg.xml中的配置组成

      <form-beans >

     <form-bean name="loginForm" type="com.proper.struts.form.LoginForm" />

    </form-beans>

    这个配置的是ActionForm

    Form-beans包含了多个form-bean

    Form-bean包含两个属性。一个name为bean的唯一标识,type为包.类名。

    <action-mappings >

        <action

          attribute="loginForm"

          input="/jsp/login.jsp"

          name="loginForm"

          path="/login"

          scope="request"

          type="com.proper.struts.action.LoginAction">

           

          <set-property property="cancellable" value="true" />

          <forward name="suc" path="/jsp/suc.jsp"/>   

        </action>

    </action-mappings>

    这个是针对Action的配置

    Action-mappings中包含多个action

    Action所包含的属性:

    Name,attribute:表示该action所对应的actionform属性。

    一个action只能对应一个form,一个form能对应多个action。

    Input:用来作为错误页的,当validate方法有错误时,则跳到这个页面。

    所有的Structs.cfg.xml下配制的路径前面必须加“/”表示在WebRoot路径下的。

    Path:表示action的虚拟路径,而且不需要加.do的后缀

    scope="request":表示action所保存的属性范围,request表示每次请求建立新的action.

    Type:表示action的包,类名。

    在action中可以包含多个不同的forward路径。

    <forward name="suc" path="/jsp/suc.jsp"/>   

    <message-resources parameter="com.proper.struts.ApplicationResources" />表示资源文件的配置。

  • 相关阅读:
    SCILAB简介[z]
    UG OPEN API编程基础 2约定及编程初步
    Office 2003与Office 2010不能共存的解决方案
    UG OPEN API 编程基础 3用户界面接口
    NewtonRaphson method
    UG OPEN API编程基础 13MenuScript应用
    UG OPEN API编程基础 14API、UIStyler及MenuScript联合开发
    UG OPEN API编程基础 4部件文件的相关操作
    UG OPEN API编程基础 1概述
    16 UG Open的MFC应用
  • 原文地址:https://www.cnblogs.com/lowerCaseK/p/struts.html
Copyright © 2011-2022 走看看