欢迎光临
我们一直在努力

struts超简单入门(三)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » struts超简单入门(三)-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章