Write three Java classes, compiled into Tomcat 5.0/webapps/struts/WEB-INF/classes/COM/javer/test/struts/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 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 () + "! Data can be stored in the database here! ");
}
}
[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 ");
}
}
This class is not required by struts. It is added to convert the encoding.
[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 = 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)
Throws servletexception
{
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
{
This. Ignore = true;
}
Else if (value. inclusignorecase ("true") | value. inclusignorecase ("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)
{
This. filterconfig = filterconfig;
}
}