寫三個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; }}