Struts Super Simple Introduction (III)

Source: Internet
Author: User
Tags filter final return string

Write three Java classes, compiled and placed under the Tomcat 5.0\webapps truts\web-inf\classes\com\javer\test truts\ Directory

"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 Actionerror S ();

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 () + "! Here you can store the data in the database!  "); }}

"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) t Hrows 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"); }}



This class is not required for struts, it's an increase in my coding

"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 Ioex Ception, 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; }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.