Announcement Announcement Module
Collapse
No announcement yet.
Trouble using JSTL to retrieve values Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble using JSTL to retrieve values

    Hello,

    This is my first time posting, so be gentle.

    I've made an attempt to start slowly migrating our Struts 1.3 code to Spring MVC 3, and I've been successful up to a point.

    When I try to retrieve variables I've set to the ModelAndView object instantiated from my controller, the JSTL tags in the JSP do not recognize their existence. I've viewed the page in debug mode, and the only instance of the saved ModelAndView object appear in a request attribute labeled "modelAndView". I can retrieve the mapping if i use that, yet it is not ideal.

    Here are snippets from my project:
    Environment:
    Code:
    Server info: IBM WebSphere Application Server/7.0
    Servlet version: 2.5
    JSP version: 2.1
    Java version: 1.6.0
    web.xml
    Code:
    <web-app id="WebApp_ID" version="2.5"
    		xmlns="http://java.sun.com/xml/ns/javaee"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
      <!-- START: Spring MVC config -->
        <servlet>
            <servlet-name>aqtWebUI</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-config/aqt/ui-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet>
            <servlet-name>aqtRESTService</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-config/aqt/REST-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet-mapping>
            <servlet-name>aqtRESTService</servlet-name>
            <url-pattern>/aqt/service/*</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>aqtWebUI</servlet-name>
            <url-pattern>/aqt/ui/*</url-pattern>
        </servlet-mapping>
    	<!-- END: Spring MVC config -->
    ui-context.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     
        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
     
        <!-- Scans within the base package of the application for @Components to configure as beans -->
        <!-- @Controller, @Service, @Configuration, etc. -->
        <context:component-scan base-package="com.bcbsfl.ecs.ecs.spss.aqt.ui" />
     
        <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
     	<!-- START: Tiles plugin -->
    	<bean id="viewResolver"
    	    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    	    <property name="viewClass">
    	        <value>org.springframework.web.servlet.view.tiles.TilesJstlView</value>
    	    </property>
    	</bean>
    	<bean id="tilesConfigurer"
    	    class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
    	    <property name="definitions">
    	        <list>
    	            <value>/WEB-INF/struts-tiles-defs/tiles-defs-aqt.xml</value>
    	            <value>/WEB-INF/struts-tiles-defs/tiles-defs-master.xml</value>
    	        </list>
    	    </property>
    	</bean>
    	<!-- END: Tiles plugin -->
    </beans>
    ManageController.java
    Code:
    @Controller
    @RequestMapping("/manage")
    public class ManageController {
    
    	@RequestMapping(value= "/list.do", method = RequestMethod.GET)
    	public ModelAndView renderList() throws Exception {
    		List<Url> urls = new ArrayList<Url>();
    		ModelAndView mav = new ModelAndView();
    		/**
    		 * for debugging only
    		 */
    		if (WebUtils.isDebug()) {
    			Url aqtUrl = new Url();
    			
    			urls.add(aqtUrl);
    			mav.addObject("aqtUrls", urls);
    		}
    		return mav;
    	}
    }
    list.jsp
    Code:
    <c:forEach var="url"
    	items="${modelAndView.model['aqtUrls'] }"
    	varStatus="urlCounter">
        <tbody class="applicant_group">
    	<tr class="applicant">
    		<td class="proposal_name">${url }</td>
            </tr>
        </tbody>
    </c:forEach>
    The JSTL in the JSP snippet above works, but this is not the intended way I had in mind to use it.

    From what I understand, I should be able to access the variable I created in my controller as ${aqtUrls} and not through the above example (${modelAndView.model['aqtUrls'] }).

    What am I missing? If there is more information you need, please let me know.

    Thanks!

  • #2
    I found the issue....

    I forgot to add the imports in my controller...rookie mistake...

    Code:
    import org.springframework.web.portlet.ModelAndView;
    I changed the import to

    Code:
    import org.springframework.web.servlet.ModelAndView;
    ...and it works fine.

    Comment

    Working...
    X