*.properties 配置 jsp.hello.title=hello world!!!! jsp.hello.error=This is error!
ActionForm
package com.yourcompany.struts.form; 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 class HelloForm extends ActionForm { private String userName; public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors=new ActionErrors(); if((userName==null)||(userName.length()<1)) { errors.add("userName",new ActionError("jsp.hello.error")); } return errors; } public void reset(ActionMapping mapping, HttpServletRequest request) { this.userName=null; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
Action就是加了一个mapping package com.yourcompany.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.yourcompany.struts.form.HelloForm; public class HelloAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm
form, HttpServletRequest request,
HttpServletResponse response) { HelloForm helloForm = (HelloForm) form;// TODO Auto-
generated method stub return mapping.findForward("Hello"); } }
jsp <%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>JSP for HelloForm form</title> </head> <body> <html:form action="/hello"> userName : <html:text
property="userName"/><html:errors property="userName"/><br/> <html:submit/><html:cancel/> </html:form> </body> </html>