Announcement Announcement Module
No announcement yet.
Quick question about Spring/Struts Controller comparison Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?


  • #2
    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.




    • #3
      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.



      • #4
        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:

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

        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:

        <B>IP Address&#58;</B>
        <spring&#58;bind path="command.ip">
          <FONT color="red">
            <B><c&#58;out value="$&#123;status.errorMessage&#125;"/></B>
          <INPUT type="text" maxlength="30" size="30" name="ip" value="<c&#58;out value="$&#123;status.value&#125;"/>" >
        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...

        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;;
           		Map model = new HashMap&#40;&#41;;
           		model.put&#40;"computer", computer&#41;;
           		return new ModelAndView&#40;"computerView", "model", model&#41;;
        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,


        • #5
          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

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

            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:

            <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            		<property name="mappings">
            				<prop key="/findComputers.htm">findComputersForm</prop>
            <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>