zoukankan      html  css  js  c++  java
  • 第一个struts程序的配置过程

    然后输入project-name,比如说“test",点finish,配置web.xml,这里的org.apache.struts.action.ActionServlet就在struts-core-1.3.10.jar中,在后面的步骤中我们要把这些jar包拷贝到WEB-INF/lib中

     1 <?xml version="1.0" encoding="ISO-8859-1"?>
     2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
     3     
     4     <servlet>
     5         <servlet-name>action</servlet-name>
     6         <servlet-class>
     7             org.apache.struts.action.ActionServlet
     8         </servlet-class>
     9         <load-on-startup>0</load-on-startup>
    10     </servlet>
    11     <servlet-mapping>
    12         <servlet-name>action</servlet-name>
    13         <url-pattern>*.do</url-pattern>
    14     </servlet-mapping>
    15     
    16 </web-app>

    配置server.xml(%CATALINA_HOME%confserver.xml),添加一个虚拟路径:

    <Context path="/test" docBase="E:web.workspace	estWebContent" reloadable="true"/>

    然后把struts的所有jar包拷贝到你的WEB-INF/lib中,对于我当前的情况,就是把

    F:\_toolsjava_libsstruts1.xstruts-1.3.10-allstruts-1.3.10lib

    给拷贝到了

    E:web.workspace estWebContentWEB-INFlib

    然后:

    你再看:现在Web App Libraries中出现了你拷贝到WEB-INFlib中的jar包

    当然,如果你还有别的需要的jar包用来在eclipse中编译一些java文件,比如servlet-api.jar和jsp-api.jar,那就通过project->properties->Java Build Path->Libraries->Add External JAR来添加,注意,这些jar包放到WEB-INFlib中是不能帮助eclipse来编译java文件的,struts那些jar包是特例

    现在你就写好你的java和各种页面文件,把java文件编译之后,拷贝到WEB-INF/classes里面即可,我这里就是:

    From: E:web.workspace estuildclassesstrutsactionHelloAction.class
      Copied to: E:web.workspace estWebContentWEB-INFclassesstrutsactionHelloAction.class
    From: E:web.workspace estuildclassesstrutsformHelloForm.class
      Copied to: E:web.workspace estWebContentWEB-INFclassesstrutsformHelloForm.class

    写好了一个hello.jsp文件,一个HelloAction.java,一个HelloForm.java,1个hello.jsp文件,功能是:从一个text框输入信息,提交给hello.jsp,如果text框中内容为空,则在hello.jsp显示错误提示信息,如果text框中内容不为空,那么就在hello.jsp页面显示text框中输入的内容

    HelloAction.java

     1 package struts.action;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 import org.apache.struts.action.Action;
     6 import org.apache.struts.action.ActionForm;
     7 import org.apache.struts.action.ActionForward;
     8 import org.apache.struts.action.ActionMapping;
     9 import struts.form.HelloForm;
    10 
    11 public class HelloAction extends Action {
    12     @Override
    13     public ActionForward execute(ActionMapping mapping, ActionForm form,
    14             HttpServletRequest request, HttpServletResponse response) {
    15         HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method
    16         String info = helloForm.getInfo(); // 所有的输入内容从ActionForm取出
    17         request.setAttribute("msg", info); // 将信息设置在request范围之中
    18         return mapping.findForward("show"); // 此处返回的是一个映射的路径
    19     }
    20 }

    HelloForm.java

     1 package struts.form;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 import org.apache.struts.action.ActionErrors;
     6 import org.apache.struts.action.ActionForm;
     7 import org.apache.struts.action.ActionMapping;
     8 import org.apache.struts.action.ActionMessage;
     9 
    10 public class HelloForm extends ActionForm {
    11 
    12     private String info;
    13 
    14     public ActionErrors validate(ActionMapping mapping,
    15             HttpServletRequest request) {
    16         ActionErrors errors = new ActionErrors();
    17         if (this.info == null || "".equals(this.info)) { // info的输入内容为空
    18             // 现在应该保存错误信息
    19             errors.add("info", new ActionMessage("error.info"));
    20         }
    21         return errors;
    22     }
    23 
    24     public void reset(ActionMapping mapping, HttpServletRequest request) {
    25     }
    26 
    27     public String getInfo() {
    28         return info;
    29     }
    30 
    31     public void setInfo(String info) {
    32         this.info = info;
    33     }
    34 }

    hello.jsp(路径为:%docBase%/hello-struts/hello.jsp)

    注意,这里的taglib在理论上来说,需要在web.xml中配置<taglib>节点来指明uri和.tld文件的映射关系,但是,因为在前面的步骤中,我们拷贝到WEB-INF/lib中有一个jar包名为“struts-taglib-1.3.10.jar”,这个jar包中有我们所需的.tld文件,并且,这些tld文件中已经定义好了我们可以使用的URI,因此,在这里我们直接用就行了,如果你不清楚应该用哪个uri,你去jar包里查看一下对应的tld文件的内容就可以找到了(在该jar包的META-INF ld文件夹中)。

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     7 <html:html lang="true">
     8 <head>
     9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    10 <title>Insert title here</title>
    11 </head>
    12 <body>
    13     <html:errors/>
    14     <logic:present name="msg" scope="request">
    15         <h2>${msg}</h2>
    16     </logic:present>
    17     <html:form action="/my-first-struts-program/hello.do" method="post">
    18         请输入信息:<html:text property="info"></html:text>
    19         <html:submit value="显示"></html:submit>
    20     </html:form>
    21 </body>
    22 </html:html>

    现在,我们需要配置WEB-INF/struts-config.xml,也就是指定我们上面写的Action和ActionForm的配置信息:

    <?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="struts.form.HelloForm" />
        </form-beans>
    
        <global-exceptions />
        <global-forwards />
        <action-mappings>
            <!-- 
                attribute|name: 都是指定此Action要使用的ActionForm的name,跟<form-bean>中的name域一致
                input: 当验证出错(ActionErrors不为空)时跳转到的错误处理页
                path: 此Action对应的虚拟路径(uri)
                scope: 此Action的作用范围,可以是request、session
                type: 此Action对应的package、class-name
            -->
            <action attribute="helloForm" input="/hello-struts/hello.jsp"
                name="helloForm" path="/my-first-struts-program/hello" scope="request"
                type="struts.action.HelloAction">
                <!--
                    name: 页面的uri
                    path: 页面的实际路径
                -->
                <forward name="show" path="/hello-struts/hello.jsp"></forward>
            </action>
        </action-mappings>
    
        <message-resources parameter="struts.ApplicationResources" />
    </struts-config>

    这里还需要说的就是,hello.jsp中action="/test/hello.do",之所以这样用,是因为<action>配置的path为/test/hello,而web.xml中配置的<url-pattern>为*.do

    另一方面,<message-resouces>的parameter指出了我们的ApplicationResources.properties文件的位置:E:web.workspace estWebContentWEB-INFclassesstrutsApplicationResources.properties

    其内容如下,当提交的信息为空的时候,显示"error.info"指定的信息:

    # 输入的信息不能为空!
    error.info = u8f93u5165u7684u4fe1u606fu4e0du80fdu4e3au7a7auff01

    测试1:输入的信息为空,直接提交

    测试2:输入一些信息,提交,用filter给request和response做一下setCharacterEncoding("UTF-8"),解决编码问题

    filter代码如下:

    package org.inf.filters;
    
    import java.io.IOException;
    import java.nio.charset.Charset;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class EncodingFilter implements Filter {
        protected String encoding = null; 
        protected FilterConfig filterConfig = null; 
        protected boolean enable = false; 
        public void destroy() { 
            this.encoding = null; 
            this.filterConfig = null; 
        } 
        public void doFilter(ServletRequest request, ServletResponse response, 
                               FilterChain chain) throws IOException, ServletException { 
            if (this.enable) {
                String encoding = this.selectEncoding(request); 
                if (encoding != null && !encoding.equals("")) { 
                    System.out.println("~~" + this + ": request :" + encoding);
                    request.setCharacterEncoding(encoding);
                }
            }
            // Pass control on to the next filter 
            chain.doFilter(request, response);
            if (this.enable) {
                String encoding = this.selectEncoding(request); 
                if (encoding != null && !encoding.equals("")) { 
                    System.out.println("~~" + this + ": response :" + encoding);
                    response.setCharacterEncoding(encoding);
                }
            }
         } 
         public void init(FilterConfig filterConfig) throws ServletException { 
             this.filterConfig = filterConfig; 
             this.encoding = filterConfig.getInitParameter("encoding");
             if (!Charset.isSupported(encoding)) {
                encoding = null;
             }
             String enableString = filterConfig.getInitParameter("enable");
             if (enableString.equalsIgnoreCase("true")) {
                this.enable = true;
             } else {
                this.enable = false;
             }
          } 
          protected String selectEncoding(ServletRequest request) { 
              return (this.encoding); 
          } 
    }

    web.xml配置:

        <filter>
            <filter-name>encoding</filter-name>
            <init-param> 
                <param-name>encoding</param-name> 
                <param-value>UTF-8</param-value> 
            </init-param> 
            <init-param> 
                <param-name>enable</param-name> 
                <param-value>true</param-value> 
            </init-param>
            <filter-class>org.inf.filters.EncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
  • 相关阅读:
    Python之matplotlib库学习
    Linux相关指令和操作
    ubuntu安装vim
    classfication中使用图像金字塔和sliding windows提高准确率
    ubuntu16.04+caffe+python接口配置
    caffe中 softmax 函数的前向传播和反向传播
    cplusplus解析
    ZStack之ZDApp_Init解析
    Z-Stack ZMain学习
    ZigBee协议学习之网络层
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3193552.html
Copyright © 2011-2022 走看看