struts超簡單入門(三)

來源:互聯網
上載者:User

寫三個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;  }}


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。