Struts ultra simple entry (3)

xiaoxiao2021-03-06  46

Write three Java classes, put it in Tomcat 5.0 / WebApps / Struts / Web-INF / CLASS / 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.rror"); return errors; }

[Hellomodel.java]:

Package com.javer.test.struts;

Public class hellomodel {public void savetopistentstore (HelloFrom HF) {system.out.println ("Hello" HF.GetPerson () "Here you can store data into 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) throws Exception {MessageResources messages = getResources (request);

ActionerroS Errors = new actionerrors (); string person = (string) PropertyUtils.getsimpleproperty (Form, "Person");

IF ("", ",", ",", "person", new actionerror ("com.javer.test.struts.Hello.unAllowed.Person, Form); SaveErrorS (Request) , Errors); Request.Removettribute (mapping.getattribute ()); return new actionforward (mapping.getinput ());}

Hellomodel hm = new hellomodel (); hm.savetopersistertore (Hellofrom);

Request.removeattribute ()); Request.SetAttribute ("Helloform", FORM);

Return mapping.findforward ("Sayhello");}}

This class is not the struts necessary, which is added to the conversion of 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 filter firmconfig = 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 filter) {this.filterconfig = filterconfig;}}

转载请注明原文地址:https://www.9cbs.com/read-40871.html

New Post(0)