You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2002/06/05 05:06:02 UTC

DO NOT REPLY [Bug 9616] New: - Some more Struts docs

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9616>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9616

Some more Struts docs

           Summary: Some more Struts docs
           Product: Struts
           Version: Nightly Build
          Platform: Other
               URL: http://jakarta.apache.org/struts/newbie.html
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Web Site
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: vic@basebeans.com


<section href="StrutsRight" name="How do I know I am doing Struts right?">
<p>

Struts is an MVC implementation. The key is to call the action first and not
call the JSP ever.<br/>
If you call the action, and it returns the JSP, you are doing MVC!.<br/>
If you link to the JSP, you are not doing MVC.<br/>
The action has a chance to pre populate the form bean, or on a submit validate
or to dispatch to another action.<br/>
You would define additional actions in action-mappings in Struts-config.xml.<br/>
</p>
</section>

<section href="QuickValidator" name="Is there a quick overview for Validator set
up?">
<p>

Make sure you have commons-validator.jar in lib.<br/>
Make sure you have Jakarta-oro.jar in lib. This is the regular expression
library.<br/>
<br/>
Add this to struts-config.
<br/>
    &lt;;!-- Validator --> <br/>
    &lt;message-resources<br/>
        parameter="ApplicationResources"/><br/>
<br/>
    &lt;!-- Validator --><br/>
    &lt;!-- Add multiple validator resource files by setting the pathname
property --><br/>
    &lt;plug-in className="org.apache.struts.validator.ValidatorPlugIn"><br/>
        &lt;set-property property="pathnames"
value="/WEB-INF/config/validator-rules.xml,<br/>
                                                
/WEB-INF/config/validation.xml"/><br/>
    &lt;/plug-in><br/>
<br/>
<br/>
ApplicationResources will contain your messages.<br/>
Validatior-Rules.xml contains validation rules.<br/>
Validaton.xml contains a list of forms and the fields to validate.<br/>
<br/>
Example Validation.xml:<br/>
<br/>
&lt;form-validation><br/>
       &lt;form name="nameZoomForm"><br/>
            &lt;field<br/>
                property="fstName"<br/>
                depends="required,mask"><br/>
               &lt;arg0 key="name.firstname.displayname"/><br/>
                &lt;var><br/>
                    &lt;var-name>mask&lt;/var-name><br/>
                    &lt;var-value>^\w+$&lt;/var-value><br/>
                &lt;/var><br/>
            &lt;/field><br/>
            &lt;field<br/>
                property="lastName"<br/>
                depends="required,mask"><br/>
                 &lt;msg<br/>
                    name="mask"<br/>
                    key="name.Last"/><br/>
                 &lt;arg0 key="name.lastname.displayname"/><br/>
                &lt;var><br/>
                   &lt;var-name>mask&lt;/var-name><br/>
                   &lt;var-value>^&lt;a-zA-Z]*$&lt;/var-value><br/>
                &lt;/var><br/>
            &lt;/field><br/>
      &lt;/form><br/>
    &lt;/formset><br/>
&lt;/form-validation><br/>
<br/>

This will use message Application Resource properties to construct the message.<br/>
<br/>
Make your form bean extend ValidatorForm:<br/>
<br/>
import org.apache.struts.action.ActionErrors;<br/>
import org.apache.struts.validator.ValidatorForm;<br/>
<br/>
public class MyFrmBean extends ValidatorForm {<br/>
// ValidatorForm base implements validate method.<br/>
<br/>
And now to use it. Note that action mapping of validate=true will turn on
automatic validation.<br/>
To manually invoke a validation in your action code something like this:<br/>
public ActionForward execute(<br/>
            ActionMapping mapping,<br/>
            ActionForm form,<br/>
            HttpServletRequest request,<br/>
            HttpServletResponse response) {<br/>

<br/>
// assume we are dispatched to a submitted save<br/>
<br/>
// cast the Struts returned form bean<br/>
  <br/>
    MyBean frm  = (MyBean) form;<br/>
//ask for the validation<br/>
        ActionErrors errors = frm.validate(mapping,request);<br/>
        
        if (errors != null andAnd !errors.empty() ) { <br/>
        //if there are errors save<br/>
           saveErrors(request, errors);<br/>
           return (mapping.findForward("display"));<br/>
 } <br/>
// frm.save();<br/>
<br/>
     return (mapping.findForward("forward"));<br/>
 } <br/>
<br/>
In your JSP page have this:<br/>
<br/>
&lt;html:errors/><br/>
<br/>
&lt;logic:messagesPresent><br/>
   &lt;bean:message key="errors.header"/><br/>
   &lt;ul><br/>
   &lt;html:messages id="error"><br/>
      &lt;li>&lt;bean:write name="error"/>&lt;/li><br/>
   &lt;/html:messages><br/>
   &lt;/ul>&lt;hr><br/>
&lt;/logic:messagesPresent><br/>
<br/><br/>
Above will display the error saved.<br/>
<br/>
To do a client side validation add this tag to your JSP page<br/>
&lt;html:javascript formName="formName" /><br/>

</p>
</section>


<section href="QuickTiles" name="Is there a quick overview for Tiles set up?">
<p>
<br/>
Make sure you have tiles.jar in lib.<br/>
<br/>
Change web.xml to:<br/>
&lt;web-app><br/>
&lt;servlet><br/>
    &lt;servlet-name>action&lt;/servlet-name><br/>
   
&lt;servlet-class>org.apache.struts.tiles.ActionComponentServlet&lt;/servlet-class><br/>
    &lt;init-param><br/>
      &lt;param-name>definitions-config&lt;/param-name><br/>
      &lt;param-value>/WEB-INF/config/tiles-defs.xml&lt;/param-value><br/>
<br/>
<br/>
And add this to Struts-config.xml:<br/>
<br/>
    &lt;controller<br/>
        processorClass="org.apache.struts.tiles.TilesRequestProcessor"/><br/>
<br/>
And create a tiles-defs like this:<br/>
&lt;tiles-definitions><br/>
  &lt;definition name="firstDef" template="/pages/common/simpleLay.jsp"><br/>
	  &lt;put name='header'  content='/pages/common/header.jsp' /><br/>
	  &lt;put name='footer'  content='/pages/common/footer.jsp' /><br/>
	  &lt;put name='leftBar'  content='/pages/common/leftBar.jsp' /><br/>
	  &lt;put name='title'   content=' Title '/><br/>
	  &lt;put name='body'    content='/pages/WelcomePg.jsp'/><br/>
   &lt;/definition><br/>
&lt;definition name="nextDef"  extends="firstDef"><br/>
          	&lt;put name='title'   content='Name'/><br/>
	  
	&lt;put name='body'    content='/pages/SrchPg.jsp'/><br/>
          &lt;/definition><br/>

<br/>

The most important setting here is the template of the layout.<br/>
Here is what simpleLay.jsp might look like:<br/>
<br/>
&lt;%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %><br/>
&lt;html><br/>
&lt;head><br/>
  &lt;title>&lt;tiles:getAsString name="title"/>&lt;/title><br/>
&lt;/head><br/>
&lt;body><br/>
&lt;div ><br/>
&lt;TABLE width="100%"><br/>
  &lt;TR><br/>
   &lt;TD colspan="2">&lt;tiles:insert attribute="header" />&lt;/TD>&lt;/TR><br/>
 &lt;TR ><br/>
   &lt;TD width="160" valign="top">&lt;tiles:insert attribute="leftBar"
/>&lt;/TD><br/>
   &lt;TD>&lt;tiles:insert attribute="body" />&lt;/TD>&lt;/TR><br/>
  &lt;TR><br/>
   &lt;TD colspan="2">&lt;tiles:insert attribute="footer" />&lt;/TD><br/>
  &lt;/TR><br/>
&lt;/TABLE><br/>
&lt;/body><br/>
&lt;/html><br/>
<br/>

See how easy it was to create a layout?<br/>
<br/>
And the definition in tiles-config.xml specifices which "tiles" to insert
where.<br/>
The next definition basically says it is the same as the first one except it has
a different main body page.<br/>
And the last part, in your action mappings, instead of forwarding to JSP,
forward to your definitoin name, like this:<br/>

&lt;forward<br/>
            name="display"<br/>
            path="nextDef"/><br/>
    &lt;/action><br/>

</p>
</section>


<section href="WhenHowStrutsFormBeans" name="When must I use the Struts Form
Beans vs. other Beans?">
<p>

If you are submitting, updating or inserting!<br/>
If you are just displaying data for read only, you can use &lt;display:tag> or
JSTL or even JSP bean define. (Note: JSTL will replace the bean and logic tags
but not the HTML tags)<br/>
It is a good idea to still populate those beans in the action, and not do that
in the JSP, and save it  in the request (or maybe session if you will need it in
the entire app. Make sure you kill session beans when they are not needed).<br/>
If you are doing read only beans as above, you do not need to define a form bean
in the action mapping.<br/>
<br/>
When you are updating you will typically need to define the form bean in the
action mapping, and then you must extend a Struts Form Bean such as
ValidatorForm.<br/>
Struts will auto-magically pass this form bean in the execute method.<br/>

public ActionForward execute(<br/>
            ActionMapping mapping,<br/>
            ActionForm form,<br/>
            HttpServletRequest request,<br/>
            HttpServletResponse response) {<br/>
// and you can get a handle to it like so.<br/>
    MyBean frm  = (MyBean) form;<br/>
Now you could do things to it, such as<br/>
frm.populate(long_id);<br/>
or<br/>
frm.populate(new PassedJavaBean(long_id));<br/>
<br/>
This update-able bean is used with the HTML page tag.<br/>
The setters for the properties for this bean are fired before the execute
method.<br/>
This is why it is a good idea that the bean always returns and takes String
arguments in its getters and setters, since HTTP is a String protocol.<br/>
Internally, the Form Bean getters and setters can be implemented using native
data types like BigDecimmal and SQL.Date.<br/>
Sometimes the getters do formatting of strings, and dates.<br/>

</p>
</section>

<section href="SinglePageMasterDetial" name="How do I do an updateable master
detail on a single page?">
<p>
For example, let's say you have an invoice and line items. Struts will iterate
all the setters for your Since there are 2 entities here, a single of one and
multiple of another, you coul have 2 beans. You can implement iterator interface
in your form beans. So in this case the "invoice" bean contains a "line_items"
bean. The line_items bean would iterate on the page usig a logic iterate tag.
</p>
</section>


<section href="ActionMessages" name="How do I pass and receive messages in the
action?">
<p>
One way is:<br/>
MyMsgBean msg = (MyMsgBean) request.getAttribute("MsgBeanName");<br/>
This assumes you set a message in request, in another action.<br/>
Same works for session.<br/>

And yet another is:<br/>
String pk = request.getParameter("pk");<br/>
This assumes you are passing in a link page tag with the parm of the primary key
the users clicked on.<br/>
<br/>

Another is:<br/>
String parm = request.getParameter("dispatch");<br/>
This assume you set a parm in the page submit tag.<br/>
This can be used to call different methods to handle SaveExecute or
InsertExecute.<br/>

</p>
</section>

<section href="JAAS" name="How do I redirect a user to a login page if the user
is not logged in?">
<p>
<br/>
The web container does this for you, using JAAS. It is best to start with that
and then extend using getUserPrincipal().<br/>
</p>
</section>
<section href="SturtsMenu" name="How Do I integrate the Struts-Menu?">
<p>
<br/>
One commonly needed component for a web applications is a centralized menu or
centralized navigation. Without this, you would put code for navigation all over
the place in many JSP pages. A typically use is to create a tile using Struts
Tiles and place a menu tag on it.<br/>
The menu tag then emits JavaScript to the browser.<br/>
See:<br/>
http://www.dhtmlcentral.com/projects/coolmenus<br/>
<br/>
You can get Struts Menu http://sourceforge.net/projects/struts-menu .<br/>
<br/>
To install Struts Menu modify your Sruts-Config.XML to add the Struts-menu.<br/>
<br/>
&lt;plug-in<br/>
className="com.fgm.web.menu.MenuPlugIn"><br/>
&lt;set-property<br/>
property="menuConfig"<br/>
value="/WEB-INF/config/menu-config.xml"/><br/>
&lt;/plug-in><br/>
<br/>
Also place the Struts-menu.jar in the Lib folder and modify web.xml to add the
Strruts Menu tag, like so.<br/>
&lt;taglib><br/>
&lt;taglib-uri>/WEB-INF/menu.tld&lt;/taglib-uri><br/>
&lt;taglib-location>/WEB-INF/lib/struts-menu.tld&lt;/taglib-location><br/>
&lt;/taglib><br/>



<br/>
This will enable the menus.<br/>
<br/>
You can look at the documentation and the sample WAR that ships with Struts-menu
that demonstrates possible displays of the menu.<br/>
The simple displayer is called "Simple".<br/>
<br/>
You place a menu tag on a tile like this:<br/>
&lt;menu:useMenuDisplayer name="Simple"><br/>
&lt;table cellpadding=2 cellspacing=2><br/>
&lt;tr>&lt;td><br/>
&lt;menu:displayMenu name="MenuToDisplay"/><br/>
&lt;/td>&lt;/tr><br/>
&lt;/table><br/>
&lt;/menu:useMenuDisplayer><br/>
<br/>
This will display the named menu.<br/>
How you need to setup up the choices to display. You do this it the
menu-config.xml.<br/>
<br/>
&lt;MenuConfig><br/>
&lt;Displayers><br/>
&lt;Displayer name="DropDown"<br/>
type="com.fgm.web.menu.displayer.DropDownMenuDisplayer"/><br/>
&lt;Displayer name="Simple"<br/>
type="com.fgm.web.menu.displayer.SimpleMenuDisplayer"/><br/>
&lt;/Displayers><br/>
&lt;Menus><br/>
&lt;Menu name="MenuNameToDisplay" title=""><br/>
&lt;Item name="NamSrch" title="Item Search"<br/>
location="srchMsg"/><br/>
&lt;Item name="ItemSrch" title="Item Search"<br/>
location="srchItem"/><br/>
&lt;/Menu><br/>
&lt;/Menus><br/>
&lt;/MenuConfig><br/>
<br/>
The Item Name is the name of menu item, and it not used in simple cases.<br/>
The Tile is what will be displayed on the menu.<br/>
The location is the path of the action mapping to be invoked.<br/>
<br/>
And here is a more complicated example.<br/>
It is possible to create drop down menus using Struts-menu.<br/>
<br/>
&lt;MenuConfig><br/>
&lt;Displayers><br/>
&lt;Displayer name="DropDown"<br/>
type="com.fgm.web.menu.displayer.DropDownMenuDisplayer"/><br/>
&lt;Displayer name="Simple"<br/>
type="com.fgm.web.menu.displayer.SimpleMenuDisplayer"/><br/>
&lt;Displayer name="CoolMenu"<br/>
type="com.fgm.web.menu.displayer.CoolMenuDisplayer"/><br/>
&lt;/Displayers><br/>
&lt;Menus><br/>
&lt;Menu name="Main" title="Main" ><br/>
&lt;Item name="MainPg" title="Main" location="mainPg.jsp"/><br/>
&lt;Item name="WhyMVC" title="Why MVC" location="whyMVC.jsp"/><br/>
&lt;Item name="Downloads" title="Downloads" location="downloads.jsp"/><br/>
&lt;/Menu><br/>
&lt;Menu name="Support" title="Support" ><br/>
&lt;Item name="DBRelated" title="DB Related" location="dbRelated.jsp"/><br/>
&lt;Item name="JSP Tags" title="JSP Tags" location="jspTags.jsp"/><br/>
&lt;Item name="Unix" title="Unix" location="unix.jsp"/><br/>
&lt;Item name="SOAP" title="SOAP" location="soap.jsp"/><br/>
&lt;Item name="SupportPg" title="Support" location="support.jsp"/><br/>
&lt;Item name="Recommendations" title="Recommendations"
location="recommendations.jsp"/><br/>
&lt;/Menu><br/>
<br/>
In above case we have 2 drop down menus we want to display side by side.
Typically you might have 4 across to display.<br/>
<br/>
You would then use a tag like this:<br/>
&lt;script language="JavaScript1.2" src="../scripts/coolmenus4.js"><br/>
&lt;/script><br/>
&lt;script src="../scripts/coolmenu-config.js"><br/>
&lt;/script><br/>
&lt;menu:useMenuDisplayer name="CoolMenu"
bundle="org.apache.struts.action.MESSAGE"><br/>
&lt;menu:displayMenu name="Main"/><br/>
&lt;menu:displayMenu name="Support"/><br/>
&lt;/menu:useMenuDisplayer><br/>
<br/>


This will emit JavaScript from your menu tag.<br/>
To set up colors and other settings you would need to edit the coolmenus4.js
configuration file, in this case places in the scripts folder in your web app.<br/>
For example:<br/>
oCMenu.barcolor="#666666" //The color of the background bar - Value: "color"<br/>
oCMenu.fromtop=0 //This is the left position of the menu. (Only in use if
menuplacement below is 0 or aligned) (will change to adapt any borders) - Value:
px || "%"<br/>
<br/>
In the future version, Stuts-menu might allow you to invoke the action's cleanup
method before going to another action by invoking the action in the paths
executeCleanUp method.<br/>
<br/>
The Struts-menu was contributed by ssayles at users.sourceforge.net.<br/>
It currently uses CoolMenu but could be configured for any menu. CoolMenu was
developed by webmaster@dhtmlcentral.com .<br/>

</p>
</section>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>