zoukankan      html  css  js  c++  java
  • 第一个Struts程序

    1.配置TOMCAT服务器:设置/mldn为访问路径,F:/mldnweb为真实路径

    2.配置Struts:

      拷贝文件:

      (1)将Struts开发包中lib目录下的全部*.jar包复制到%TOMCAT_HOME%lib目录中,即F:mldnwebWEB-INFlib目录中(或D:ProgramFilesTomcat 8.5lib目录中)

      (2)将Struts开发包中lib目录下的struts-taglib-1.3.10.jar文件解压,将解压后struts-taglib-1.3.10META-INF ld目录下的所有*.tld文件拷贝至F:mldnwebWEB-INF目录下(该目录位置与后续F:mldnwebWEB-INF目录中web-xml中配置的tld文件路径位置对应即可),包括struts-bean.tld、struts-html.tld及struts-logic.tld等

    3.配置文件:

      (1)F:mldnwebWEB-INF目录中web-xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>1</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>
    <jsp-config>
    <taglib>
    <taglib-uri>http://struts.apache.org/tags-bean</taglib-uri>
    <taglib-location>/WEB-INF/strus-bean.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>http://struts.apache.org/tags-html</taglib-uri>
    <taglib-location>/WEB-INF/strus-html.tld</taglib-location>
    </taglib>
    <taglib>
    <taglib-uri>http://struts.apache.org/tags-logic</taglib-uri>
    <taglib-location>/WEB-INF/strus-logic.tld</taglib-location>
    </taglib>
    </jsp-config>
    </web-app>

      (2)F:mldnwebWEB-INF目录中struts-config.xml文件:

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

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

    <struts-config>
    <form-beans>
    <form-bean name="helloForm"
    type="org.lxh.struts.form.HelloForm" />
    </form-beans>

    <global-exceptions />
    <global-forwards />
    <action-mappings>
    <action attribute="helloForm" input="/hello.jsp"
    name="helloForm" path="/hello" scope="request"
    type="org.lxh.struts.action.HelloAction">
    <forward name="show" path="/hello.jsp"></forward>
    </action>
    </action-mappings>

    <message-resources parameter="org.lxh.struts.ApplicationResources" />
    </struts-config>

    3.编写代码:

      (1)F:mldnweb目录中的hello.jsp文件:

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

    <%@ 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"%>
    <html:html lang="true">
    <head>
    <title>hello.jsp</title>
    </head>
    <body>
    <html:errors/>
    <logic:present name="msg" scope="request">
    <h2>${msg}</h2>
    </logic:present>
    <html:form action="hello.do" method="post">
    请输入信息:<html:text property="info"></html:text>
    <html:submit value="显示"></html:submit>
    </html:form>
    </body>
    </html:html>

      (2)F:mldnwebWEB-INFclasses目录中的HelloAction.java文件:

    /*
    * Generated by MyEclipse Struts
    * Template path: templates/java/JavaClass.vtl
    */
    package org.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 org.lxh.struts.form.HelloForm;

    /**
    * MyEclipse Struts Creation date: 07-09-2009
    *
    * XDoclet definition:
    *
    * @struts.action path="/hello" name="helloForm" input="/hello.jsp"
    * scope="request" validate="true"
    */
    public class HelloAction 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) {
    HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method
    String info = helloForm.getInfo(); // 所有的输入内容从ActionForm取出
    request.setAttribute("msg", info); // 将信息设置在request范围之中
    return mapping.findForward("show"); // 此处返回的是一个映射的路径
    }
    }

      (3)F:mldnwebWEB-INFclasses目录中的HelloForm.java文件:

    /*
    * Generated by MyEclipse Struts
    * Template path: templates/java/JavaClass.vtl
    */
    package org.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-09-2009
    *
    * XDoclet definition:
    *
    * @struts.form name="helloForm"
    */
    public class HelloForm extends ActionForm {
    /*
    * Generated fields
    */

    /** info property */
    private String info;

    /*
    * Generated Methods
    */

    /**
    * Method validate
    *
    * @param mapping
    * @param request
    * @return ActionErrors
    */
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (this.info == null || "".equals(this.info)) { // info的输入内容为空
    // 现在应该保存错误信息
    errors.add("info", new ActionMessage("error.info"));
    }
    return errors;
    }

    /**
    * Method reset
    *
    * @param mapping
    * @param request
    */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    // TODO Auto-generated method stub
    }

    /**
    * Returns the info.
    *
    * @return String
    */
    public String getInfo() {
    return info;
    }

    /**
    * Set the info.
    *
    * @param info
    * The info to set
    */
    public void setInfo(String info) {
    this.info = info;
    }
    }

    4.打包编译:将上述HelloAction.java及HelloForm.java文件打包编译(javac -d . *.java)

    5.重启TOMCAT,测试成功

  • 相关阅读:
    简便的将DataSet导入到数据库中
    数据类型的小小研究:Access与SQL Server的数据类型
    【jxust acm 20120708】
    【D ECJTU_ACM 11级队员2012年暑假训练赛(2)】
    【hdu 2101 A + B Problem Too】
    【hdu 1014】
    【hdu 1164 Eddy's research I】
    【开始,安全编程】
    【hdu 1285 确定比赛名次】
    【hdu 1163 Eddy's digital Roots 】
  • 原文地址:https://www.cnblogs.com/huhewei/p/12885484.html
Copyright © 2011-2022 走看看