[ajug-members] Re: nov: struts+jsp+jstl+hibernate

Dan Marchant driedtoast at gmail.com
Fri Jun 2 00:55:33 EDT 2006


StrutsTestCase is not to bad for testing struts.
Try it out. You can also use mockobjects and have a request/response
mock that you feed into your action for some validation and create a
mock ActionMapping object that validates the mapping.findForward
method to see if the behavior is what you expected.

Also cactus is something to look into as well a bit more to setup but
gives you in container testing.

Hope this helps.

- Dan

On 6/1/06, J. Talafous <jtalafous at gmail.com> wrote:
> That "Jakarta Struts Cookbook" is wonderful, it answered my question
> exactly.  Thanks for referring it to me.  I can't wait until the
> author is in Atlanta for a book signing!
>
> Here's yet another question.  Before I "code myself into a corner", I
> need to start thinking about setting up a testing environment so I can
> pass the project around to others.  I remember hearing that Struts is
> difficult to perform unit testing.  Is that true?  What do you
> recommend?  I don't want to delve into Spring (yet), but do I need to?
>
> Thanks!
>
> On 6/1/06, Bill Siggelkow <bsiggelkow at mac.com> wrote:
> > Joe,
> >
> > Try setting the indexed property on the <html:text> tag like this:
> >
> > <html:text property="answers" value="${poll.answers[i]}"
> > indexed="true"/>
> >
> > This should render the text field with a name like 'answers[0]',
> > 'answers[1]', ...
> >
> > Alternatively you can skip the <html:text> altogether ...
> >
> > <input type="text" name="answers[${i}]" value="${poll.answers[i]}"/>
> >
> > There's a wonderful book called the "Jakarta Struts Cookbook" -- I
> > can't remember who wrote it but it is awesome (even better than the
> > DaVinci code IMO). Check out page 61.
> >
> > BTW, if you use an ActionForm -- prepopulating it in the
> > SelectPollAction -- you do not need to explicitly use the value=$
> > {poll.... } statements. Struts will populate the form for you. Then,
> > on validation failures, the form will properly display the user's
> > prior input.
> >
> > -Bill Siggelkow
> >
> >
> > > <html:text property="answers[${i}]"
> > > value="${poll.answers[i]}"/>
> >
> >
> >
> > > <c:forEach begin="0" end="${poll.answerCount-1}" var="i" >
> > > Answer ${i+1}: <html:text property="answers[${i}]"
> > > value="${poll.answers[i]}"/> <br>
> > > </c:forEach>
> >
> >
> > On May 31, 2006, at 9:58 PM, J. Talafous wrote:
> >
> > > Thanks, that worked great!  But I have another question...
> > >
> > > Here's the web flow so far:
> > >
> > > 1.) Index.jsp has a href link to an action that gets all Polls from
> > > PollDAO, puts the List of Polls into request scope and forwards to
> > > SelectPoll.jsp.
> > >
> > > 2.) SelectPoll.jsp page prints out all the Polls ith radio buttons:
> > > ...
> > > <H1>Select an existing poll</H1>
> > > <html:form action="SelectPoll">
> > > <c:forEach items="${polls}" var="poll">
> > > <html:radio property="pollId" value="${poll.id}"/><c:out
> > > value="${poll}"></c:out><br>
> > > </c:forEach>
> > > <H3>or create a new one</H3>
> > > <html:radio property="pollId" value="0"/><c:out value="Create new
> > > poll"></c:out><br>
> > > <html:submit />
> > > </html:form >
> > > ...
> > >
> > > and it works great!  User selects a Poll (as described by its
> > > toString) with the radio button and pushes Submit, which passes
> > > "pollId" thru request parameter to SelectPollAction.  No ActionForm
> > > bean used.
> > >
> > > 3.) SelectPollAction uses "pollId" to get that Poll from the PollDAO,
> > > puts it into the request and forwards  (via struts-config.xml stuff)
> > > to modify_poll.jsp:
> > > ...
> > > <html:form action="ModifyPoll">
> > > Poll #: <c:out value="${poll.id}"></c:out><br>
> > > Question: <html:text property="question" value="${poll.question}"/
> > > ><br>
> > > Description: <html:text property="description" value="$
> > > {poll.description}"/><br>
> > > <c:forEach begin="0" end="${poll.answerCount-1}" var="i" >
> > > Answer ${i+1}: <html:text property="answers${i}"
> > > value="${poll.answers[i]}"/> <br>
> > > </c:forEach>
> > > <html:text property="ends" value="${poll.ends}"/>
> > > <html:submit value="Save modification"/>
> > > </html:form >
> > > ...
> > >
> > > I don't know if this is the proper way to do this jsp, but it displays
> > > properly.  There are as many textfields as in the answer string array
> > > in the Poll.  Note the inside of the <foreach> loop.
> > >
> > > The problem I am having now is constructing the ModifyPollForm bean
> > > that will be read in the  modifyPollAction.  Given <html:text
> > > property="answerXXX">, the textfields will be hardcoded in the HTML,
> > > it has to be hardcoded as a property in the ActionForm too.(?)  But
> > > when the next Poll and JSP come along, that ActionForm isn't
> > > necessarily valid, unless I impose a maximum number of answers, which
> > > is probably a kludge.
> > >
> > > I figure that Struts has to take care of this one way or another.  Is
> > > there a special tag to do this?
> > > For example: http://www.jguru.com/faq/view.jsp?EID=915898 may
> > > recommend this tag:
> > >   <logic:iterate>
> > > But I am hesitant to use any non-JSTL tag besides <html:XXX>.
> > >
> > > What is the proper way to populate a Struts ActionForm given a
> > > variable-length array of Strings in a class like Poll?
> > >
> > > This novice thanks you in advance!
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > On 5/30/06, Bill Siggelkow <bsiggelkow at mac.com> wrote:
> > >>
> > >>
> > >> Umm ... I'm not too sure where session-scope falls into this --
> > >> but you want the flow to be like this:
> > >>
> > >>
> > >> 1) User accesses URL to retrieve polls -- http://foo.bar.com/
> > >> getPolls.do
> > >> 2) In  struts-config you map /getPolls to GetPollsAction class
> > >> 3) In GetPollsAction you call some service class that calls the
> > >> PollDao.findAllPolls (which uses hibernate) to get the polls
> > >> 4) The in the action you set the polls as a request attribute and
> > >> then forward (mapping.findForward("success") or similar) to the JSP
> > >> 5) On the JSP you render the list of polls using <c:forEach
> > >> items="polls" var="poll"> ...
> > >>
> > >>
> > >> In general, you don't want Hibernate-specific stuff in the Action
> > >> class -- this should be in a "service" layer class. You should not
> > >> be importing any Hibernate specific APIs in your Struts actions.
> > >>
> > >>
> > >> BTW, sounds loike you are on the right track.
> > >>
> > >>
> > >> -Bill Siggelkow
> > >>
> > >>
> > >>
> > >>
> > >> On May 29, 2006, at 7:15 PM, J. Talafous wrote:
> > >>
> > >>
> > >>
> > >> One idea is to do the hibernate work in the Action controller
> > >> inside session scope  before forwarding to the page so you can use
> > >> the JSP/JSTL taglib as regular.
> > >>
> > >>
> > >> On 5/29/06,  J. Talafous <jtalafous at gmail.com> wrote:
> > >> >
> > >> > Ok, this novice know just enough to be dangerous.  I would like
> > >> to know the *proper* way to do the following before someone loses
> > >> an eye:
> > >> >
> > >> > 1. We have a struts app that uses the action mapping to call up
> > >> a servlet or jsp.
> > >> >
> > >> > 2. This servlet or jsp needs to write out a page that allows the
> > >> user to select from many objects (specifically polls, but later
> > >> will be others) that are retrieved from hibernate.  The page makes
> > >> a table with the objects string rep in rows on this table for
> > >> example.
> > >> >
> > >> > 3. Should I use a servlet to write the page above?  If so,
> > >> accessing hibernate is no prob.  Should I use JSP?  If so, how do
> > >> I get at hibernate?  Is there a taglib I need to be aware of?
> > >> SHould I not use hibernate and use JSTL instead?  I would like to
> > >> use hibernate.
> > >> >
> > >> > Hope I am not being too vague.
> > >> > Thanks!
> > >> >
> > >>
> > >>
> > >>
> > >> _______________________________________________
> > >> ajug-members mailing list
> > >> ajug-members at ajug.org
> > >> http://www.ajug.org/mailman/listinfo/ajug-members
> > >>
> > >> _______________________________________________
> > >> ajug-members mailing list
> > >> ajug-members at ajug.org
> > >> http://www.ajug.org/mailman/listinfo/ajug-members
> > >>
> > >>
> > >>
> > > _______________________________________________
> > > ajug-members mailing list
> > > ajug-members at ajug.org
> > > http://www.ajug.org/mailman/listinfo/ajug-members
> >
> > _______________________________________________
> > ajug-members mailing list
> > ajug-members at ajug.org
> > http://www.ajug.org/mailman/listinfo/ajug-members
> >
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>



More information about the ajug-members mailing list