·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » struts超简单入门(三)

struts超简单入门(三)

类别: JAVA教程  评论数:0 总得分:0

写三个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;  }}



-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1