Struts super simple entry (3)

Source: Internet
Author: User

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;
}
}

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.