Struts is an open source framework for building Java-based web applications. It is maintained by the Apache Software Foundation.
The Structs framework features the MVC architecture where "Model" (data) and "View" (data presentation) are completely separated by a "Controller". Struts has a controller which supports various model techniques and view techniques. Supported model techniques include but not limited to JDBC, EJB, Hibernate, Object Relational Bridge, etc. Supported view techniques include JSP, JSTL, JSF, Velocity, XSLT, etc. [STRUTSWEBSITE] 2 A brief history of Java-based web application develoepment At first it was plain HTML, where information flow is almost uni-directional. The only feedback from the browser is the link to the next page. Then people invented CGI through which browsers can submit their information. Java servlet is built atop CGI and provides a faster, more convenient engine. JSP then relieved developers from embedding HTML tags in servlet classes and allows them to freely mix HTML and java code together. JSP-based web application development is the so-called Model 1. The problem with model one is that JSP doesnot provide too much for business flow control. Therefore, people tried to use Servlet and JSP together: using servlet to control business logic, while leaving the tedious HTML presentation work to JSP, this is the so-called Model 2 Stuts is a framework supporting "Model 2" java-based web application developement. 3 How struts connects a web page with backend actions? In WEB-INF/web.xml, add the struts action handler <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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> Use the struts-config.xml file. This file connects front end pages (forms) with backend classes. The following example has been adapted from Reuman.net: <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" > <struts-config> <!-- Form Bean Definitions --> <form-beans> <form-bean name="demoForm" type="com.foo.bar.DemoForm"/> </form-beans> <!-- Action Mapping Definitions --> <action-mappings> <action path="/setupDemoForm" forward="/demoForm.jsp"/> <action path="/insertDemo" type="com.foo.bar.InsertDemoAction" name="demoForm" scope="request" validate="false" > <forward name="success" path="/confirm.jsp"/> </action> </action-mappings> <!-- message resources --> <message-resources parameter="ApplicationResources" null="false" /> </struts-config> The form class "com.foo.bar.DemoForm" shall extend ActionForm class from the struts package, and explicitly define setters and getters for each variable in your form (the form in your jsp file). Therefore, a form class is a Javabean from this point of view. The action class, "com.foo.bar.InsertDemoAction", shall extend "org.apache.struts.actions.LookupDispatchAction", the reloadable method public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception gives your action class access to the ActionForm you just implemented, which contains getters and setters to values from your form. To process different actions (multiple submit buttons), use getKeyMethodMap(). 4 How to use tiles to organize your web pages Use structs tiles to organized your jsp pages. The following is an example tiles config file. This file is called tiles-def.xml, and is usually in your WEB-INF directory. <definition name=".view.penguins" extends=".basic.layout"> <put name="title" value="Welcome to the Penguin Store."/> <put name="content" value="/jsp/viewPenguins.jsp"/> </definition> 5 Hello World! struts example Note that code in this example has been adapted from http://benmira.free.fr/en/j2ee/struts1.htm. Comments has been removed, please refer to the original tutorial for detailed comments. check to make sure that you have struts installed check to make sure that you have a servlet container installed. In my case, I used Tomcat create a directory strutstest in $TOMCAT_HOME/webapps create or copy necessary files to the test directory. Finally, you directry structure should look like this: $TOMCAT_HOME/webapps |---META-INF | |---MANIFEST.MF |---WEB-INF | |---classes | | |---HelloForm.java | | |---HelloAction.java | | |---HelloModel.java | | |---Constants.java | |---lib | | |---struts.jar | | |---commons-beanutils.jar | |---struts-bean.tld | |---struts-html.tld | |---struts-logic.tld | |---struts-nested.tld | |---struts-tiles.tld | |---web.xml | |---struts-config.xml | |---Application.properties |---helloworld.jsp add the following code into helloworld.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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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> Note that we are using struts tags instead of regular HTML tags so that this jsp file can work with struts. Please refer to struts manuals for the meanings of these tags. add the following into Application.properties 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 name to say hello to : examples.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!! examples.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to! These are the resources used either by helloworld.jsp or the classes coming up. Add the following code into HelloForm.java import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public final class HelloForm extends ActionForm { private String person = null; public String getPerson() { return (this.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 ((person = = null) || (person.length() < 1)) errors.add("person", new ActionError("examples.hello.no.person.error")); return errors; } } Put the following code into HelloAction.java import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.util.*; 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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } } Add the following code into HelloModel.java /** *<p>This is a Model object which simply contains the name of the person we * want to say "Hello!" to.<p> * * In a more advanced application, this Model component might update * a persistent store with the person name, use it in an argument in a web * service call, or send it to a remote system for processing. * */ public class HelloModel { private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } } Add the following code to Contants.java public final class Constants { public static final String HELLO_KEY = "examples.hello"; } Put the following into 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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="examples.hello.HelloForm"/> </form-beans> <action-mappings> <action path="/HelloWorld" type="examples.hello.HelloAction" name= "HelloForm" scope="request" validate="true" input="/hello.jsp"> <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings> <message-resources parameter="examples.hello.Application"/> </struts-config> try it out. if you see the following error: exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) root cause java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs. 6 Cannot find bean xxxx in any scope Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
At first it was plain HTML, where information flow is almost uni-directional. The only feedback from the browser is the link to the next page. Then people invented CGI through which browsers can submit their information. Java servlet is built atop CGI and provides a faster, more convenient engine. JSP then relieved developers from embedding HTML tags in servlet classes and allows them to freely mix HTML and java code together.
JSP-based web application development is the so-called Model 1. The problem with model one is that JSP doesnot provide too much for business flow control. Therefore, people tried to use Servlet and JSP together: using servlet to control business logic, while leaving the tedious HTML presentation work to JSP, this is the so-called Model 2
Stuts is a framework supporting "Model 2" java-based web application developement. 3 How struts connects a web page with backend actions? In WEB-INF/web.xml, add the struts action handler <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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> Use the struts-config.xml file. This file connects front end pages (forms) with backend classes. The following example has been adapted from Reuman.net: <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" > <struts-config> <!-- Form Bean Definitions --> <form-beans> <form-bean name="demoForm" type="com.foo.bar.DemoForm"/> </form-beans> <!-- Action Mapping Definitions --> <action-mappings> <action path="/setupDemoForm" forward="/demoForm.jsp"/> <action path="/insertDemo" type="com.foo.bar.InsertDemoAction" name="demoForm" scope="request" validate="false" > <forward name="success" path="/confirm.jsp"/> </action> </action-mappings> <!-- message resources --> <message-resources parameter="ApplicationResources" null="false" /> </struts-config> The form class "com.foo.bar.DemoForm" shall extend ActionForm class from the struts package, and explicitly define setters and getters for each variable in your form (the form in your jsp file). Therefore, a form class is a Javabean from this point of view. The action class, "com.foo.bar.InsertDemoAction", shall extend "org.apache.struts.actions.LookupDispatchAction", the reloadable method public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception gives your action class access to the ActionForm you just implemented, which contains getters and setters to values from your form. To process different actions (multiple submit buttons), use getKeyMethodMap(). 4 How to use tiles to organize your web pages Use structs tiles to organized your jsp pages. The following is an example tiles config file. This file is called tiles-def.xml, and is usually in your WEB-INF directory. <definition name=".view.penguins" extends=".basic.layout"> <put name="title" value="Welcome to the Penguin Store."/> <put name="content" value="/jsp/viewPenguins.jsp"/> </definition> 5 Hello World! struts example Note that code in this example has been adapted from http://benmira.free.fr/en/j2ee/struts1.htm. Comments has been removed, please refer to the original tutorial for detailed comments. check to make sure that you have struts installed check to make sure that you have a servlet container installed. In my case, I used Tomcat create a directory strutstest in $TOMCAT_HOME/webapps create or copy necessary files to the test directory. Finally, you directry structure should look like this: $TOMCAT_HOME/webapps |---META-INF | |---MANIFEST.MF |---WEB-INF | |---classes | | |---HelloForm.java | | |---HelloAction.java | | |---HelloModel.java | | |---Constants.java | |---lib | | |---struts.jar | | |---commons-beanutils.jar | |---struts-bean.tld | |---struts-html.tld | |---struts-logic.tld | |---struts-nested.tld | |---struts-tiles.tld | |---web.xml | |---struts-config.xml | |---Application.properties |---helloworld.jsp add the following code into helloworld.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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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> Note that we are using struts tags instead of regular HTML tags so that this jsp file can work with struts. Please refer to struts manuals for the meanings of these tags. add the following into Application.properties 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 name to say hello to : examples.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!! examples.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to! These are the resources used either by helloworld.jsp or the classes coming up. Add the following code into HelloForm.java import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public final class HelloForm extends ActionForm { private String person = null; public String getPerson() { return (this.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 ((person = = null) || (person.length() < 1)) errors.add("person", new ActionError("examples.hello.no.person.error")); return errors; } } Put the following code into HelloAction.java import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.util.*; 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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } } Add the following code into HelloModel.java /** *<p>This is a Model object which simply contains the name of the person we * want to say "Hello!" to.<p> * * In a more advanced application, this Model component might update * a persistent store with the person name, use it in an argument in a web * service call, or send it to a remote system for processing. * */ public class HelloModel { private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } } Add the following code to Contants.java public final class Constants { public static final String HELLO_KEY = "examples.hello"; } Put the following into 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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="examples.hello.HelloForm"/> </form-beans> <action-mappings> <action path="/HelloWorld" type="examples.hello.HelloAction" name= "HelloForm" scope="request" validate="true" input="/hello.jsp"> <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings> <message-resources parameter="examples.hello.Application"/> </struts-config> try it out. if you see the following error: exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) root cause java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs. 6 Cannot find bean xxxx in any scope Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
<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> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" > <struts-config> <!-- Form Bean Definitions --> <form-beans> <form-bean name="demoForm" type="com.foo.bar.DemoForm"/> </form-beans> <!-- Action Mapping Definitions --> <action-mappings> <action path="/setupDemoForm" forward="/demoForm.jsp"/> <action path="/insertDemo" type="com.foo.bar.InsertDemoAction" name="demoForm" scope="request" validate="false" > <forward name="success" path="/confirm.jsp"/> </action> </action-mappings> <!-- message resources --> <message-resources parameter="ApplicationResources" null="false" /> </struts-config>
<action path="/insertDemo" type="com.foo.bar.InsertDemoAction" name="demoForm" scope="request" validate="false" > <forward name="success" path="/confirm.jsp"/> </action> </action-mappings> <!-- message resources --> <message-resources parameter="ApplicationResources" null="false" /> </struts-config>
public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception
4 How to use tiles to organize your web pages Use structs tiles to organized your jsp pages. The following is an example tiles config file. This file is called tiles-def.xml, and is usually in your WEB-INF directory. <definition name=".view.penguins" extends=".basic.layout"> <put name="title" value="Welcome to the Penguin Store."/> <put name="content" value="/jsp/viewPenguins.jsp"/> </definition> 5 Hello World! struts example Note that code in this example has been adapted from http://benmira.free.fr/en/j2ee/struts1.htm. Comments has been removed, please refer to the original tutorial for detailed comments. check to make sure that you have struts installed check to make sure that you have a servlet container installed. In my case, I used Tomcat create a directory strutstest in $TOMCAT_HOME/webapps create or copy necessary files to the test directory. Finally, you directry structure should look like this: $TOMCAT_HOME/webapps |---META-INF | |---MANIFEST.MF |---WEB-INF | |---classes | | |---HelloForm.java | | |---HelloAction.java | | |---HelloModel.java | | |---Constants.java | |---lib | | |---struts.jar | | |---commons-beanutils.jar | |---struts-bean.tld | |---struts-html.tld | |---struts-logic.tld | |---struts-nested.tld | |---struts-tiles.tld | |---web.xml | |---struts-config.xml | |---Application.properties |---helloworld.jsp add the following code into helloworld.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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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> Note that we are using struts tags instead of regular HTML tags so that this jsp file can work with struts. Please refer to struts manuals for the meanings of these tags. add the following into Application.properties 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 name to say hello to : examples.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!! examples.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to! These are the resources used either by helloworld.jsp or the classes coming up. Add the following code into HelloForm.java import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public final class HelloForm extends ActionForm { private String person = null; public String getPerson() { return (this.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 ((person = = null) || (person.length() < 1)) errors.add("person", new ActionError("examples.hello.no.person.error")); return errors; } } Put the following code into HelloAction.java import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.util.*; 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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } } Add the following code into HelloModel.java /** *<p>This is a Model object which simply contains the name of the person we * want to say "Hello!" to.<p> * * In a more advanced application, this Model component might update * a persistent store with the person name, use it in an argument in a web * service call, or send it to a remote system for processing. * */ public class HelloModel { private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } } Add the following code to Contants.java public final class Constants { public static final String HELLO_KEY = "examples.hello"; } Put the following into 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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="examples.hello.HelloForm"/> </form-beans> <action-mappings> <action path="/HelloWorld" type="examples.hello.HelloAction" name= "HelloForm" scope="request" validate="true" input="/hello.jsp"> <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings> <message-resources parameter="examples.hello.Application"/> </struts-config> try it out. if you see the following error: exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) root cause java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs. 6 Cannot find bean xxxx in any scope Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
Use structs tiles to organized your jsp pages. The following is an example tiles config file. This file is called tiles-def.xml, and is usually in your WEB-INF directory.
<definition name=".view.penguins" extends=".basic.layout"> <put name="title" value="Welcome to the Penguin Store."/> <put name="content" value="/jsp/viewPenguins.jsp"/> </definition>
5 Hello World! struts example Note that code in this example has been adapted from http://benmira.free.fr/en/j2ee/struts1.htm. Comments has been removed, please refer to the original tutorial for detailed comments. check to make sure that you have struts installed check to make sure that you have a servlet container installed. In my case, I used Tomcat create a directory strutstest in $TOMCAT_HOME/webapps create or copy necessary files to the test directory. Finally, you directry structure should look like this: $TOMCAT_HOME/webapps |---META-INF | |---MANIFEST.MF |---WEB-INF | |---classes | | |---HelloForm.java | | |---HelloAction.java | | |---HelloModel.java | | |---Constants.java | |---lib | | |---struts.jar | | |---commons-beanutils.jar | |---struts-bean.tld | |---struts-html.tld | |---struts-logic.tld | |---struts-nested.tld | |---struts-tiles.tld | |---web.xml | |---struts-config.xml | |---Application.properties |---helloworld.jsp add the following code into helloworld.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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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> Note that we are using struts tags instead of regular HTML tags so that this jsp file can work with struts. Please refer to struts manuals for the meanings of these tags. add the following into Application.properties 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 name to say hello to : examples.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!! examples.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to! These are the resources used either by helloworld.jsp or the classes coming up. Add the following code into HelloForm.java import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public final class HelloForm extends ActionForm { private String person = null; public String getPerson() { return (this.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 ((person = = null) || (person.length() < 1)) errors.add("person", new ActionError("examples.hello.no.person.error")); return errors; } } Put the following code into HelloAction.java import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.util.*; 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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } } Add the following code into HelloModel.java /** *<p>This is a Model object which simply contains the name of the person we * want to say "Hello!" to.<p> * * In a more advanced application, this Model component might update * a persistent store with the person name, use it in an argument in a web * service call, or send it to a remote system for processing. * */ public class HelloModel { private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } } Add the following code to Contants.java public final class Constants { public static final String HELLO_KEY = "examples.hello"; } Put the following into 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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="examples.hello.HelloForm"/> </form-beans> <action-mappings> <action path="/HelloWorld" type="examples.hello.HelloAction" name= "HelloForm" scope="request" validate="true" input="/hello.jsp"> <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings> <message-resources parameter="examples.hello.Application"/> </struts-config> try it out. if you see the following error: exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) root cause java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs. 6 Cannot find bean xxxx in any scope Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
Note that code in this example has been adapted from http://benmira.free.fr/en/j2ee/struts1.htm. Comments has been removed, please refer to the original tutorial for detailed comments.
$TOMCAT_HOME/webapps |---META-INF | |---MANIFEST.MF |---WEB-INF | |---classes | | |---HelloForm.java | | |---HelloAction.java | | |---HelloModel.java | | |---Constants.java | |---lib | | |---struts.jar | | |---commons-beanutils.jar | |---struts-bean.tld | |---struts-html.tld | |---struts-logic.tld | |---struts-nested.tld | |---struts-tiles.tld | |---web.xml | |---struts-config.xml | |---Application.properties |---helloworld.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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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>
<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="examples.hello" scope="request"> <h2> Hello <bean:write name="examples.hello" property="person" />!<p> </h2> </logic:present> <html:form action="/HelloWorld.do?action=gotName" focus="username" > <bean:message key="hello.jsp.prompt.person"/> <html:text property="person" 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>
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 name to say hello to : examples.hello.dont.talk.to.atilla=I told you not to talk to Atilla!!! examples.hello.no.person.error=Please enter a <i>PERSON</i> to say hello to!
import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.*; public final class HelloForm extends ActionForm { private String person = null; public String getPerson() { return (this.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 ((person = = null) || (person.length() < 1)) errors.add("person", new ActionError("examples.hello.no.person.error")); return errors; } }
import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; import org.apache.struts.util.*; 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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); } HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } }
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"); String badPerson = "Atilla the Hun"; if (person.equals(badPerson)) { errors.add("person", new ActionError("examples.hello.dont.talk.to.atilla", badPerson )); saveErrors(request, errors); return (new ActionForward(mapping.getInput())); }
HelloModel hm = new HelloModel(); hm.setPerson(person); hm.saveToPersistentStore(); request.setAttribute( Constants.HELLO_KEY, hm); request.removeAttribute(mapping.getAttribute()); return (mapping.findForward("SayHello")); } }
/** *<p>This is a Model object which simply contains the name of the person we * want to say "Hello!" to.<p> * * In a more advanced application, this Model component might update * a persistent store with the person name, use it in an argument in a web * service call, or send it to a remote system for processing. * */ public class HelloModel { private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } }
private String _person = null; public String getPerson() { return this._person; } public void setPerson(String person) { this._person = person; } public void saveToPersistentStore() { } }
public final class Constants { public static final String HELLO_KEY = "examples.hello"; }
<?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="examples.hello.HelloForm"/> </form-beans> <action-mappings> <action path="/HelloWorld" type="examples.hello.HelloAction" name= "HelloForm" scope="request" validate="true" input="/hello.jsp"> <forward name="SayHello" path="/hello.jsp" /> </action> </action-mappings> <message-resources parameter="examples.hello.Application"/> </struts-config>
exception org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) root cause java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
org.apache.jasper.JasperException org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
java.lang.NullPointerException org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1161) org.apache.struts.taglib.TagUtils.message(TagUtils.java:1024) org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:224) org.apache.jsp.helloworld_jsp._jspx_meth_bean_message_0(org.apache.jsp.helloworld_jsp:189) org.apache.jsp.helloworld_jsp._jspx_meth_html_html_0(org.apache.jsp.helloworld_jsp:133) org.apache.jsp.helloworld_jsp._jspService(org.apache.jsp.helloworld_jsp:103) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
6 Cannot find bean xxxx in any scope Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
Check to make sure that xxxx, the bean property, doesnot start with a Capital letter. Struts checks that. For example, if you have a property named "Abc", although you have a getter getAbc in your bean class (your ActionForm class), struts won't associate Abc with getAbc() because it thinks that getAbc() is for the "abc" property. 7 No getter or setter method defined for property XXX in bean YYY check and make sure that you have getXXX method and setXXX method in your Form class (which is defined in WEB-INF/struts-config.xml) if you already have the getter and setter method, check to make sure that you have restarted your jboss server after you last modified struts-config.xml. New config will not take effect until you restart jboss. 8 How do I output some text information in struts Use the html:text tag, for example: <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table> 9 "Cannot retrieve mapping for action XXX" Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
Use the html:text tag, for example:
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table>
<table border="0"> <tr> <td>Status:</td> <td><html:text property="emailStatus" /></td> </tr> </table>
Check to make sure you have a correct action="" item in your struts form definition 10 How to output plain text information in struts Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
Use html:message 11 DispatchMapping does not define a handler property If your action is extending LookupDispatchAction, make sure you have parameter='cmd' in your action definition in struts-config.xml. For example, [TT] <?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config> 12 missing resource in key method map Check You have the correct ApplicationResource.properties file, and you have confiured correctly in the web.xml file <message-resources parameter="com.company.project.ApplicationResources" /> check to make sure that you defined correct getKeyMethodMap function something like the following protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; } this is weired, but I once remove all my libraries and then replaced them and fixed the problem 13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
If your action is extending LookupDispatchAction, make sure you have
parameter='cmd'
<?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"> <struts-config> <form-beans> <form-bean name="HelloForm" type="com.foo.bar.HelloForm"/> </form-beans> <action-mappings> <action path="/hello" parameter="cmd" type="com.foo.bar.HelloForm" name="HelloForm" scope="request" validate="false" > <forward name="Save" path="/saved.jsp" /> </action> </action-mappings> <message-resources parameter="com.foo.bar.Hello"/> </struts-config>
Check
<message-resources parameter="com.company.project.ApplicationResources" />
protected Map<String,String> getKeyMethodMap() { Map<String,String> map = new HashMap<String,String>(); map.put("link.load","load"); return map; }
13 Servlet action not found To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
To diagnose this problem, you need to look at the server start log. If you are using "nohup ./run.sh", then it's in nohup.out in your jboss bin directory. In my case, I am missing commons-digester.jar. Downloaded a copy from apache commons website and then struts worked fine. 14 Useful struts links struts html tags Comments #1, at Nov 29, 2006 02:33:59, somebody said: Article wass useful References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917
References[STRUTSWEBSITE] http://struts.apache.org/[TT] http://www.tek-tips.com/viewthread.cfm?qid=802917