zoukankan      html  css  js  c++  java
  • Struts 基础开发---day01

     前言

    我是一个小白,今天首次学习Struts的基本内容,是看视频和看书相结合的,主要是记录这个学习的过程以及学习的过程中遇到的问题。

    -----------------------------------------------------------------------------------------------------------------------------------

    传统的MVC分为servlet(控制器),Javabean(模型层),jsp(显示层)三个部分,Struts可以说是MVC的一个具体的实现,增加了ActionForm、Action和Struts标签库层。接下来用myEclipse开发第一个Struts程序,大致流程为:在hello.jsp页面通过文本框输入要显示的内容,然后提交到struts,如果内容为空,点击显示按钮,出现错误提示;如果不为空,struts将信息显示在页面上。

    file--->new--->webproject,这里将名称取为strutstest,然后点击此项目右键,--->myEclipse--->Add Struts Capabilities,这里是添加了struts支持 。

    点击finish之后在项目下可以看到struts-config.xml   web.xml  JavaEE的jar包以及Struts的jar包等。

    删除原来的index.jsp  在Web-Root下新建一个hello.jsp

    接下来新建ActionForm及Action。注意每一个处理类Action都要绑定一个ActionFrom。

    选中刚刚的com.du.struts包--->new --->other ,再搜索Struts,选择Struts1.3下的struts1.3 From,Action&JSP

    点击next

    点击finish之后,配置文件里面的form-beans和action-mappings有新的内容。其中path表示提交的路径,input表示错误信息的显示页面。在web.xml里面的url-pattern是*.do,那么在hello.jsp里面的action就是hello.do.大体框架已经搭好了,下面就是进行逻辑上的操作。我把各部分的代码放在下面供参考:

     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 
     3 <!-- 这是Struts已经添加好的标签库,我们直接使用即可 -->
     4 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
     5 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
     6 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
     7 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
     8 
     9 
    10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11 <html:html lang="true">
    12   <head>
    13    
    14     <title>hello.jsp</title>
    15 
    16   </head>
    17   
    18   <body>
    19       <html:errors/>
    20       <logic:present name="msg" scope="request">
    21       <h2>${msg}</h2>
    22       </logic:present>
    23    <html:form action="" method="post">
    24        请输入信息:<html:text property="info"></html:text>
    25        <html:submit value="显示"></html:submit>
    26    </html:form>
    27   </body>
    28 </html:html>
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
     3 
     4 <struts-config>
     5   <form-beans >
     6     <form-bean name="helloForm" type="com.du.struts.form.HelloForm" />
     7 
     8   </form-beans>
     9 
    10   <global-exceptions />
    11   <global-forwards />
    12   <action-mappings >
    13     <action
    14       attribute="helloForm"
    15       input="/hello.jsp"
    16       name="helloForm"
    17       path="/hello"
    18       scope="request"
    19       type="com.du.struts.action.HelloAction"
    20       cancellable="true" >
    21       <forward name="show" path="/hello.jsp"></forward>
    22       </action>
    23    
    24   </action-mappings>
    25 
    26   <message-resources parameter="com.du.struts.ApplicationResources" />
    27 </struts-config>
    config Code
     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 
     3 <!-- 这是Struts已经添加好的标签库,我们直接使用即可 -->
     4 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
     5 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
     6 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
     7 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
     8 
     9 
    10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11 <html:html lang="true">
    12   <head>
    13    
    14     <title>hello.jsp</title>
    15 
    16   </head>
    17   
    18   <body>
    19       <html:errors/>
    20       <logic:present name="msg" scope="request">
    21       <h2>${msg}</h2>
    22       </logic:present>
    23    <html:form action="" method="post">
    24        请输入信息:<html:text property="info"></html:text>
    25        <html:submit value="显示"></html:submit>
    26    </html:form>
    27   </body>
    28 </html:html>
    HelloJsp Code
     1 /*
     2  * Generated by MyEclipse Struts
     3  * Template path: templates/java/JavaClass.vtl
     4  */
     5 package com.du.struts.form;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import org.apache.struts.action.ActionErrors;
     9 import org.apache.struts.action.ActionForm;
    10 import org.apache.struts.action.ActionMapping;
    11 import org.apache.struts.action.ActionMessage;
    12 
    13 /** 
    14  * MyEclipse Struts
    15  * Creation date: 07-20-2017
    16  * 
    17  * XDoclet definition:
    18  * @struts.form name="helloForm"
    19  */
    20 public class HelloForm extends ActionForm {
    21     /*
    22      * Generated fields
    23      */
    24 
    25     /** info property */
    26     private String info;
    27 
    28     /*
    29      * Generated Methods
    30      */
    31 
    32     /** 
    33      * Method validate
    34      * @param mapping
    35      * @param request
    36      * @return ActionErrors
    37      */
    38     //ActionFrom类用于验证,info属性与表单提交的参数名称一致,并设置了setter和getter操作
    39     public ActionErrors validate(ActionMapping mapping,
    40             HttpServletRequest request) {
    41         ActionErrors errors=new ActionErrors();
    42         if(this.info==null||"".equals(this.info)){//info的输入内容为空
    43             //保存错误信息,一个ActionErrors可以包含多个ActionMessage,
    44             //ActionMessage类的构造方法中需要传递一个指定错误信息的key,错误信息在ApplicationResource.properties中定义
    45             //资源文件不支持中文,会将中文自动转换为Unicode编码
    46             errors.add("info",new ActionMessage("error.info"));
    47         }
    48         return errors;
    49     }
    50 
    51     /** 
    52      * Method reset
    53      * @param mapping
    54      * @param request
    55      */
    56     public void reset(ActionMapping mapping, HttpServletRequest request) {
    57         // TODO Auto-generated method stub
    58     }
    59 
    60     /** 
    61      * Returns the info.
    62      * @return String
    63      */
    64     public String getInfo() {
    65         return info;
    66     }
    67 
    68     /** 
    69      * Set the info.
    70      * @param info The info to set
    71      */
    72     public void setInfo(String info) {
    73         this.info = info;
    74     }
    75 }
    HelloFrom Code
     1 /*
     2  * Generated by MyEclipse Struts
     3  * Template path: templates/java/JavaClass.vtl
     4  */
     5 package com.du.struts.action;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import org.apache.struts.action.Action;
    10 import org.apache.struts.action.ActionForm;
    11 import org.apache.struts.action.ActionForward;
    12 import org.apache.struts.action.ActionMapping;
    13 import com.du.struts.form.HelloForm;
    14 
    15 /** 
    16  * MyEclipse Struts
    17  * Creation date: 07-20-2017
    18  * 
    19  * XDoclet definition:
    20  * @struts.action path="/hello" name="helloForm" input="/hello.jsp" scope="request" validate="true"
    21  */
    22 public class HelloAction extends Action {
    23     /*
    24      * Generated Methods
    25      */
    26 
    27     /** 
    28      * Method execute
    29      * @param mapping
    30      * @param form
    31      * @param request
    32      * @param response
    33      * @return ActionForward
    34      */
    35     public ActionForward execute(ActionMapping mapping, ActionForm form,
    36             HttpServletRequest request, HttpServletResponse response) {
    37         HelloForm helloForm = (HelloForm) form;// HelloFrom对象
    38         String info=helloForm.getInfo();//所有的输入内容从ActionFrom取出
    39         request.setAttribute("msg", info);//设置request的属性范围,另外每一个Action都需要一个跳转路径,到配置文件去设置
    40         return mapping.findForward("show");
    41     }
    42 }
    HelloAction Code
    1 error.info=u8F93u5165u4FE1u606Fu4E0Du80FDu4E3Au7A7AuFF01
    Resource Code

    不好意思,补充一下,那个hello.jsp里面的action是hello.do  写忘记了

    写的过程中所犯的错误:

    ActionFrom类里面的错误信息保存New Message 不是Messages啊

    ------------------------------------------------------------------------------------------------------------

    在Action里面写一个输入长度不超过15的错误集合

     1 /*
     2  * Generated by MyEclipse Struts
     3  * Template path: templates/java/JavaClass.vtl
     4  */
     5 package com.yourcompany.struts.action;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import org.apache.struts.action.Action;
    10 import org.apache.struts.action.ActionForm;
    11 import org.apache.struts.action.ActionForward;
    12 import org.apache.struts.action.ActionMapping;
    13 import org.apache.struts.action.ActionMessage;
    14 import org.apache.struts.action.ActionMessages;
    15 
    16 import com.yourcompany.struts.form.LalaForm;
    17 
    18 /** 
    19  * MyEclipse Struts
    20  * Creation date: 07-20-2017
    21  * 
    22  * XDoclet definition:
    23  * @struts.action path="/lala" name="lalaForm" input="/lala.jsp" scope="request" validate="true"
    24  */
    25 public class LalaAction extends Action {
    26     /*
    27      * Generated Methods
    28      */
    29 
    30     /** 
    31      * Method execute
    32      * @param mapping
    33      * @param form
    34      * @param request
    35      * @param response
    36      * @return ActionForward
    37      */
    38     public ActionForward execute(ActionMapping mapping, ActionForm form,
    39             HttpServletRequest request, HttpServletResponse response) {
    40         LalaForm lalaForm = (LalaForm) form;// TODO Auto-generated method stub
    41         String info=lalaForm.getInfo();
    42         if(info.length()>15){//输入内容过长
    43             ActionMessages errors=new ActionMessages();//定义错误集合,ActionErrors已被弃用
    44             errors.add("info", new ActionMessage("length.info"));//添加一个新的错误
    45             //保存错误,没有用saveErrors()方法,所以jsp里面的错误信息要修改,用<html:message>标签
    46             super.saveMessages(request,errors);
    47             return mapping.getInputForward();//跳转到input指定页面
    48         }else {
    49             request.setAttribute("msg", info);//设置request属性范围
    50 
    51         }
    52         return mapping.findForward("show");//进行页面跳转
    53     }
    54 }
    Action Code
     1 <%@ page language="java" pageEncoding="GBK"%>
     2 
     3 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
     4 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
     5 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
     6 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
     7 
     8 
     9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10 <html:html lang="true">
    11   <head>
    12     
    13     <title>lala.jsp</title>
    14 
    15   </head>
    16    
    17   <body>
    18       <html:messages id="info" message="true">
    19       <h2>${info}</h2>
    20       </html:messages>
    21       
    22       <html:errors/>
    23       <logic:present name="msg" scope="request">
    24             <h2>${msg}</h2>
    25       </logic:present>
    26       
    27     <html:form action="lala.do" method="post">
    28     请输入信息:<html:text property="info"></html:text>
    29     <html:submit value="显示"></html:submit>
    30     </html:form>
    31   </body>
    32 </html:html>
    JSP Code
  • 相关阅读:
    使用 yo 命令行向导给 SAP UI5 应用添加一个新的视图
    SAP Fiori Elements 应用的 manifest.json 文件运行时如何被解析的
    SAP UI5 标准应用的多语言支持
    微软 Excel 365 里如何设置下拉菜单和自动高亮成指定颜色
    SAP Fiori Elements 应用里的 Title 显示的内容是从哪里来的
    本地开发好的 SAP Fiori Elements 应用,如何部署到 ABAP 服务器上?
    如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息
    教你一招:让集群慢节点无处可藏
    应用架构步入“无服务器”时代 Serverless技术迎来新发展
    MySQL数据库事务隔离性的实现
  • 原文地址:https://www.cnblogs.com/dublogs/p/7211570.html
Copyright © 2011-2022 走看看