Results 1 to 6 of 6

Thread: Quick question about Spring/Struts Controller comparison

  1. #1

    Default Quick question about Spring/Struts Controller comparison

    In Struts if you want to have mulitple return views in Struts, you just add more lines to your struts-config.xml for instance:
    <forward name="success" path="/WEB-INF/results/result1.jsp"/>
    <forward name="baddata" path="/WEB-INF/results/result2.jsp"/>

    But in the Spring examples I've seen, I've only seen a success view written in the action-servlet.xml like:
    <property name="successView"><value>index.jsp</value></property>

    Is there a way to specify the "error" views in Spring the xml without having to hard-code them in the Controller class?

    Thanks,
    PJ

  2. #2
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    You could add an errorView property to your controller and associated get/setErrorView methods. Then add this as a property in your context XML file.

    HTH,

    Matt

  3. #3

    Default

    Matt you're everywhere, this is the same guy that asked you about the ldap stuff this past weekend I have it implemented using a LdapDAO, but it's not quite as slick as I'd like it to be just yet. I'll probably post a thread about it later this evening in the Architecture Discussion forum here if you're curious. Thanks for the response BTW.

    -PJ

  4. #4

    Default

    Hey PJ,

    I'm just becoming familiar with Spring myself, but here is what I've learned from my first app...
    There are a couple of things you can do. First, you can define an ExceptionResolver in your servlet context file:

    Code:
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    		<property name="exceptionMappings">
    			<props>
    				<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
    				<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
    			</props>
    		</property>
    </bean>
    Then, depending on how you set up your ViewResolver, you just set the URL of the dataAccessFailure view. If you're using ResourceBundleViewResolver, your views.properties file would contain something like the following:

    Code:
    dataAccessFailure.class=org.springframework.web.servlet.view.JstlView
    dataAccessFailure.url=/WEB-INF/jsp/dataAccessFailure.jsp
    Using this method, you can "catch" any RuntimeException and "throw" it to a specific view, jsp page in this case.

    As a second point, if the "baddata" you are refering to is the failure to pass a validator, then the same view will be redisplayed. If you've bound in any error messages, they would then appear. Here is an example using jsp. It is a simple form field expecting an IP address:

    Code:
    <B>IP Address&#58;</B>
    <spring&#58;bind path="command.ip">
      <FONT color="red">
        <B><c&#58;out value="$&#123;status.errorMessage&#125;"/></B>
      </FONT>
      <INPUT type="text" maxlength="30" size="30" name="ip" value="<c&#58;out value="$&#123;status.value&#125;"/>" >
    </spring&#58;bind>
    <P>
    If on validation, the controller throws a validation error, this jsp will display that error message.

    The other thing I've seen is to have your controller handle specific data by returning a Redirection view...

    Code:
    public ModelAndView computerHandler&#40;HttpServletRequest request, HttpServletResponse response&#41; throws ServletException &#123;
       		Computer computer = getApplicationFacade&#40;&#41;
                   .loadComputer&#40;RequestUtils.getIntParameter&#40;request, "computerId", 0&#41;&#41;;
       		if &#40;computer == null&#41; &#123;
       			return new ModelAndView&#40;"findComputersRedirect"&#41;;
       		&#125;
       		Map model = new HashMap&#40;&#41;;
       		model.put&#40;"computer", computer&#41;;
       		return new ModelAndView&#40;"computerView", "model", model&#41;;
     &#125;
    In this case, the controller looks for a QueryString called computerId, and uses it to look up a computer in the database. If it doesn't return a computer, the controller then redirects it to the view called "findComputersRedirect".

    Like I said, I'm by no means an expert at Spring yet, but I hope this helps you a bit.

    Take care,
    James

  5. #5

    Default

    All very helpful, thanks James. One question I did have, though, concerning your "findComputersRedirect" view. Would you specify that in action-servlet.xml with something along these lines?

    <property name="findComputersRedirect"><value>foo.jsp</value></property>

  6. #6

    Default

    Hey,

    "findComputersRedirect" is actually a view name that is defined, in this case, within the views.properties file:

    Code:
    findComputersRedirect.class=org.springframework.web.servlet.view.RedirectView
    findComputersRedirect.url=findComputers.htm
    The reason is to resend, in this case, http://host/appName/findComputers.htm which is then re-handled by the servlet's mappings. It is in the servlet's mappings that findComputers.htm is handed over to the corresponding controller bean:

    Code:
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/findComputers.htm">findComputersForm</prop>
    			</props>
    		</property>
    	</bean
    
    <bean id="findComputersForm" class = "application.web.spring.FindComputersForm">
    		<property name="formView"><value>findComputersForm</value></property>
    		<property name="selectView"><value>selectComputerView</value></property>
    		<property name="successView"><value>computerRedirect</value></property>
    </bean>
    -James

Similar Threads

  1. Question on Multi-Action Controller
    By jcheung in forum Container
    Replies: 3
    Last Post: Nov 26th, 2009, 06:58 AM
  2. OT: quick question any java CIFS implementations?
    By Colin Yates in forum Architecture
    Replies: 2
    Last Post: Aug 2nd, 2005, 03:01 AM
  3. Quick question about subflows
    By Colin Yates in forum Web Flow
    Replies: 2
    Last Post: Jul 1st, 2005, 03:24 AM
  4. Quick Question
    By twoencore in forum Architecture
    Replies: 4
    Last Post: May 16th, 2005, 12:26 PM
  5. Newbie Question: General Controller / Manager?
    By pulse1014 in forum Architecture
    Replies: 14
    Last Post: Dec 17th, 2004, 03:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •