struts超简单入门(三)

80酷酷网    80kuku.com

写三个java类,编译后放到Tomcat 5.0\webapps truts\WEB-INF\classes\com\javer\test truts\目录下

【HelloFrom.java】:

package com.javer.test.struts;

import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionError;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;

public final class HelloFrom    extends ActionForm{  private String person = null;  public String getPerson()  {    return person;  }

  public void setPerson(String person)  {    this.person = person;  }

  public void reset(ActionMapping mapping, HttpServletRequest request)  {    this.person = null;  }

  public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)  {    ActionErrors errors = new ActionErrors();

    if(this.person==null || this.person.length()<1)      errors.add("person",new ActionError("com.javer.test.struts.hello.error"));    return errors;  }}

【HelloModel.java】:

package com.javer.test.struts;

public class HelloModel{  public void saveToPersistentStore(HelloFrom hf)  {    System.out.println("Hello "+hf.getPerson()+"!这里可存储数据到数据库中!");  }}

【HelloAction.java】:

package com.javer.test.struts;

import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;import org.apache.struts.action.ActionError;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;

import org.apache.struts.util.MessageResources;

import org.apache.commons.beanutils.PropertyUtils;

public final class HelloAction    extends Action{  public ActionForward execute(ActionMapping mapping, ActionForm form,                               HttpServletRequest request,                               HttpServletResponse response)      throws Exception  {    MessageResources messages = getResources(request);

    ActionErrors errors = new ActionErrors();    String person = (String) PropertyUtils.getSimpleProperty(form, "person");

    if(person.indexOf(",")!=-1)    {      errors.add("person",new ActionError("com.javer.test.struts.hello.unallowed.person",form));      saveErrors(request,errors);      request.removeAttribute(mapping.getAttribute());      return new ActionForward(mapping.getInput());    }

    HelloModel hm = new HelloModel();    hm.saveToPersistentStore((HelloFrom)form);

    request.removeAttribute(mapping.getAttribute());    request.setAttribute("helloForm",form);

    return mapping.findForward("SayHello");  }}



这个类不是struts必需的,是我为了转化编码而增加的

【EncodingFilter.java】:

package com.javer.test.struts;

import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;

public class EncodingFilter    implements Filter{  protected String encoding = null;

  protected FilterConfig filterConfig = null;

  protected boolean ignore = true;

  public void destroy()  {    this.encoding = null;    this.filterConfig = null;  }

  public void doFilter(      ServletRequest request,      ServletResponse response,      FilterChain chain)      throws IOException, ServletException  {    if (ignore || (request.getCharacterEncoding() == null))    {      request.setCharacterEncoding(selectEncoding(request));    }    chain.doFilter(request, response);  }

  public void init(FilterConfig filterConfig)      throws ServletException  {

    this.filterConfig = filterConfig;    this.encoding = filterConfig.getInitParameter("encoding");    String value = filterConfig.getInitParameter("ignore");    if (value == null)    {      this.ignore = true;    }    else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))    {      this.ignore = true;    }    else    {      this.ignore = false;    }  }

  protected String selectEncoding(ServletRequest request)  {    return (this.encoding);  }

  public FilterConfig getFilterConfig()  {    return filterConfig;  }

  public void setFilterConfig(FilterConfig filterConfig)  {    this.filterConfig = filterConfig;  }}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: