4、application.properties #Application Resource for the "Hello" sample application hello.jsp.title=Hello A first struts program hello.jsp.page.heading=Hello World!A first Struts application hello.jsp.prompt.person=Please enter a UserName to say Hello to: hello.jsp.page.hello=Hello#Validation and error message for HelloForm.java and HelloAction.java hello.dont.talk.monster=We don't want to say hello to Monster!!! hello.no.username.error=Please enter a <i> UserName </i> to say hello to! 5、Constant.java package hello;public final class Constants { /** *The application scope attribute under which our user database *is stored. */ public static final String PERSON_KEY="personbean"; } 6、HelloAction。java package hello;import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.MessageResources;public final class HelloAction extends Action { /** *Process the specified HTTP request,and create the corresponding HTTP *response (or forward to another web component that will create it). *control should be forward,or<code>null</code>if the response has *already been completed. */ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { //These "message" come from the ApplicationResource.properties file MessageResources messages=getResources(request); /* *Validate the request parameters specified by the user *Note:Basic field validation done in HelloForm.java * */ ActionMessages errors=new ActionMessages(); String userName=(String)((HelloForm)form).getUserName(); String badUserName="Monster"; if(userName.equalsIgnoreCase(badUserName)) { errors.add("username",new ActionMessage("hello.dont.talk.to.monster",badUserName)); //saveErrors(request,errors); return(new ActionForward(mapping.getInput())); } /* *Having received and validated the data submitted *from the View.we now update the model */ PersonBean pb=new PersonBean(); pb.setUserName(userName); pb.saveToPersistentStore(); /* *if there was a choice of View components that depended on the model *(or some other)status.we'd make the decision here as to which *to display.In this case.there is only one View component. * */ request.setAttribute(Constants.PERSON_KEY,pb); //Remove the Form Bean-don't need to carry values forward request.removeAttribute(mapping.getAttribute()); //Forward control to the specified success URI return (mapping.findForward("SayHello")); } } 7、HelloForm.java package hello;import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping;public final class HelloForm extends ActionForm { private String userName=null; public String getUserName() { return (this.userName); } public void setUserName(String userName) { this.userName=userName; } /** *Reset all properties to their default values. */ public void reset(ActionMapping mapping,HttpServletRequest request) { this.userName=null; } /** *Validate the properties posted in this request.if validation errors are *found,return an <code>ActionError</code>object containing the errors. *if no validation errors occur,return <code>null</code> or an empty *<code>ActionErrors</code>object. */ public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if((userName==null)||(userName.length()<1)) { errors.add("username",new ActionMessage("hello.no.username.error")); } return errors; } } 8、PersonBean.java package hello;public class PersonBean { private String userName=null; public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName=userName; } /** *This is a stub method that would be used for the Model to save *the information submitted to a persistent store.in this sample *application it is not used. */ public void saveToPersistentStore() { /* *This is a stub method that might be used to save the person's *name to a persistent store(i.e.database) if this were a real application. * *The actual business operations that would exist within a Model *component would depend upon the requirements of the application. */ } }
我就是搞不清楚为什么老是出现这样的提示: Unable to initialize TldLocationsCache: nul 请高手们继续帮助我啊
下面我就把代码贴上来
1、hello.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><html:html locale="true">
<head>
<title><bean:message key="hello.jsp.title"/></title>
<html:base/>
</head> <body bgcolor="white"><p>
<h2><bean:message key="hello.jsp.page.heading"/></h2><p>
<html:errors/><p> <logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean write name="personbean" property="userName"/>!<p>
</h2>
</logic:present> <html:form action="/HelloWorld.do" focus="userName">
<bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/><br>
<html:submit property="submit" value="Submit"/>
<html:reset/>
</html:form><br> <html:img page="/struts-power.gif" alt="Powered by Struts"/>
</body>
</html:html>
2、struts_config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><!--
This is the Struts configuration file for the "Hello!" sample application
-->
<struts-config> <!--FormBean Definitions-->
<form-beans>
<form-bean name="HelloForm" type="hello.HelloForm"/>
</form-beans> <!--Action Mapping Definitions-->
<action-mappings>
<!--Say Hello!-->
<action path = "/HelloWorld"
type = "hello.HelloAction"
name = "HelloForm"
scope = "request"
validation = "true"
input = "/hello.jsp"
>
<forward name="SayHello" path="/hello.jsp"/>
</action>
</action-mappings> <!--Message Resource Definitions-->
<message-resources parameter="hello.application"/>
</struts-config>
3、web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app>
<display-name>HelloApp Struts Application</display-name> <!--Standard Action Servlet Configuration-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <!--Standard Action Servlet Mapping-->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!--The Usual Welcome FileList-->
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list> <!--Struts Tag Library Description-->
<taglib>
<taglib-url>/WEB-INF/struts-bean.tld</taglib-url>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib> <taglib>
<taglib-url>/WEB-INF/struts-html.tld</taglib-url>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> <taglib>
<taglib-url>/WEB-INF/struts-logic.tld</taglib-url>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib> <taglib>
<taglib-url>/WEB-INF/struts-nested.tld</taglib-url>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib> <taglib>
<taglib-url>/WEB-INF/struts-tiles.tld</taglib-url>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib> </web-app>
#Application Resource for the "Hello" sample application
hello.jsp.title=Hello A first struts program
hello.jsp.page.heading=Hello World!A first Struts application
hello.jsp.prompt.person=Please enter a UserName to say Hello to:
hello.jsp.page.hello=Hello#Validation and error message for HelloForm.java and HelloAction.java
hello.dont.talk.monster=We don't want to say hello to Monster!!!
hello.no.username.error=Please enter a <i> UserName </i> to say hello to!
5、Constant.java
package hello;public final class Constants
{
/**
*The application scope attribute under which our user database
*is stored.
*/
public static final String PERSON_KEY="personbean";
}
6、HelloAction。java
package hello;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;public final class HelloAction extends Action
{
/**
*Process the specified HTTP request,and create the corresponding HTTP
*response (or forward to another web component that will create it).
*control should be forward,or<code>null</code>if the response has
*already been completed.
*/ public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception
{
//These "message" come from the ApplicationResource.properties file
MessageResources messages=getResources(request); /*
*Validate the request parameters specified by the user
*Note:Basic field validation done in HelloForm.java
*
*/ ActionMessages errors=new ActionMessages();
String userName=(String)((HelloForm)form).getUserName(); String badUserName="Monster"; if(userName.equalsIgnoreCase(badUserName))
{
errors.add("username",new ActionMessage("hello.dont.talk.to.monster",badUserName));
//saveErrors(request,errors);
return(new ActionForward(mapping.getInput()));
} /*
*Having received and validated the data submitted
*from the View.we now update the model
*/ PersonBean pb=new PersonBean();
pb.setUserName(userName);
pb.saveToPersistentStore(); /*
*if there was a choice of View components that depended on the model
*(or some other)status.we'd make the decision here as to which
*to display.In this case.there is only one View component.
*
*/ request.setAttribute(Constants.PERSON_KEY,pb); //Remove the Form Bean-don't need to carry values forward
request.removeAttribute(mapping.getAttribute()); //Forward control to the specified success URI
return (mapping.findForward("SayHello"));
}
}
7、HelloForm.java
package hello;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class HelloForm extends ActionForm
{
private String userName=null; public String getUserName()
{
return (this.userName);
} public void setUserName(String userName)
{
this.userName=userName;
} /**
*Reset all properties to their default values.
*/ public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.userName=null;
} /**
*Validate the properties posted in this request.if validation errors are
*found,return an <code>ActionError</code>object containing the errors.
*if no validation errors occur,return <code>null</code> or an empty
*<code>ActionErrors</code>object.
*/ public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
ActionErrors errors = new ActionErrors(); if((userName==null)||(userName.length()<1))
{
errors.add("username",new ActionMessage("hello.no.username.error"));
}
return errors;
}
}
8、PersonBean.java
package hello;public class PersonBean
{
private String userName=null; public String getUserName()
{
return this.userName;
} public void setUserName(String userName)
{
this.userName=userName;
} /**
*This is a stub method that would be used for the Model to save
*the information submitted to a persistent store.in this sample
*application it is not used.
*/ public void saveToPersistentStore()
{
/*
*This is a stub method that might be used to save the person's
*name to a persistent store(i.e.database) if this were a real application.
*
*The actual business operations that would exist within a Model
*component would depend upon the requirements of the application.
*/
}
}
请高手们继续帮助我啊