爱心技术专栏专题

struts超简单入门(三)

摘录:java基础 来源:java基础 加入时间:2007年03月08日
摘要:
struts超简单入门(三)

 

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

【HelloFrom.java】…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info8/a_8b380690bb3f0bc0.htm

struts超简单入门(三)

站点:爱心种子小博士 关键字:struts超简单入门(三)

   
struts超简单入门(三)
 

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

【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;
  }
}


客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: