PDA

View Full Version : Trouble getting JSTL working correctly, not parsing values.


jcblitz
Jul 18th, 2007, 10:31 AM
For some reason I can't get JSTL working. From what I've read and have done in the past, this looks right to me. The only thing I can come up with is there is something wrong with the JSTL dependency from maven2 (I'm just stabbing in the dark). The dependencies I'm using for JSTL are

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

In my x-servlet.xml I have

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>

And in a controller:
public ModelAndView home(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelAndView view = new ModelAndView("hello");
view.addObject("welcome", "This is just a JSTL test");
return view;
}

and in hello.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>Dashboard</title></head>
<body>
<h1>Hello - Welcome to the dashboard</h1>
<c:out value="welcome" />
${welcome}
</body>
</html>

Which prints out "welcome ${welcome}"

Any ideas from anyone else using maven2?

pmularien
Jul 18th, 2007, 11:55 AM
<c:out value="welcome" />
This should be:
<c:out value="${welcome}" />
This is a very common problem, and unlikely to be related to Maven. What app server are you using? The most common causes are (1) not running an app server that uses JSP 2.x, (2) incorrect webapp version (< 2.4) in web.xml, or (3) incorrect taglib directive in the JSP.

Please check your web.xml and tell us what webapp version you're using, and post back with answers to the question above if you still aren't able to get it working.

jcblitz
Jul 18th, 2007, 12:07 PM
ah, thank you! It was set as a 2.3 web app instead of a 2.4. I'm not sure how that happened, in eclipse I told it to use the 2.4 facet but yet it somehow generated a 2.3 web.xml. Thanks again, I didn't even think of checking that.

wowiesy
Aug 4th, 2007, 11:49 AM
<c:out value="welcome" />
This should be:
<c:out value="${welcome}" />
This is a very common problem, and unlikely to be related to Maven. What app server are you using? The most common causes are (1) not running an app server that uses JSP 2.x, (2) incorrect webapp version (< 2.4) in web.xml, or (3) incorrect taglib directive in the JSP.

Please check your web.xml and tell us what webapp version you're using, and post back with answers to the question above if you still aren't able to get it working.

How do I setup a webapp 2.4 template (dtd or xsd) in eclipse? where do I get the dtd/xsd file? I've been trying to test a simple setup and my taglibs in the JSP are not rendering properly. I'm using Java6/tomcat 5.5/Spring 2.0 / Postgresql ..

jcblitz
Aug 4th, 2007, 07:27 PM
How do I setup a webapp 2.4 template (dtd or xsd) in eclipse? where do I get the dtd/xsd file? I've been trying to test a simple setup and my taglibs in the JSP are not rendering properly. I'm using Java6/tomcat 5.5/Spring 2.0 / Postgresql ..

I'm not sure what you mean by template, but it's in web.xml

<web-app id="myfriendslist" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

...

</web-app>

wowiesy
Aug 4th, 2007, 11:48 PM
In Eclipse, when I create a new XML file (File -> New -> Other -> XML), I am presented with a catalog of XMLs, I guess Eclipse uses the catalog (which is in essence a dtd file or an xsd file) to validate the created XML as you type it.

Initially, I setup Eclipse to use webapp_2_3.dtd. I was able to successfully use this to create a web.xml which was eventually successfully packaged and deployed into Tomcat 5.5.23.

However, my JSPs aren't rendering properly. Specifically, the taglibs are not being rendered. I added a <taglib-location> and <taglib-uri> in the web.xml and still the tags are not recognized (I'm using <c:forEach> and <c:out> tags).

From the post above (and others I found on the Net), it points me to use ver 2.4. I found a webapp_2_4.xsd which I can use in Eclipse to validate as I type. Here is the web.xml I use:

<?xml version="1.0" encoding="UTF-8"?>
<j2ee:web-app version="2.4"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/webapp2_4.xsd ">

<j2ee:display-name>Daily Sales Monitoring Application</j2ee:display-name>
<j2ee:description>Daily Sales Monitoring Application</j2ee:description>
<j2ee:context-param>
<j2ee:param-name>contextConfigLocation</j2ee:param-name>
<j2ee:param-value>/WEB-INF/applicationContext.xml</j2ee:param-value>
</j2ee:context-param>
<j2ee:listener>
<j2ee:listener-class>org.springframework.web.context.ContextLoaderListe ner</j2ee:listener-class>
</j2ee:listener>
<j2ee:servlet>
<j2ee:servlet-name>dsrapp</j2ee:servlet-name>
<j2ee:servlet-class>org.springframework.web.servlet.DispatcherServlet</j2ee:servlet-class>
<j2ee:init-param>
<j2ee:param-name>detectAllHandlerAdapters</j2ee:param-name>
<j2ee:param-value>false</j2ee:param-value>
</j2ee:init-param>
<j2ee:init-param>
<j2ee:param-name>detectAllHandlerExceptionResolvers</j2ee:param-name>
<j2ee:param-value>false</j2ee:param-value>
</j2ee:init-param>
<j2ee:init-param>
<j2ee:param-name>detectAllHandlerMappings</j2ee:param-name>
<j2ee:param-value>true</j2ee:param-value>
</j2ee:init-param>
<j2ee:init-param>
<j2ee:param-name>detectAllViewResolvers</j2ee:param-name>
<j2ee:param-value>true</j2ee:param-value>
</j2ee:init-param>
<j2ee:load-on-startup>1</j2ee:load-on-startup>
</j2ee:servlet>
<j2ee:servlet-mapping>
<j2ee:servlet-name>dsrapp</j2ee:servlet-name>
<j2ee:url-pattern>/app/*</j2ee:url-pattern>
</j2ee:servlet-mapping>
<j2ee:welcome-file-list>
<j2ee:welcome-file>index.jsp</j2ee:welcome-file>
</j2ee:welcome-file-list>
<j2ee:jsp-config>
<j2ee:taglib>
<j2ee:taglib-uri>http://java.sun.com/jsp/jstl/core</j2ee:taglib-uri>
<j2ee:taglib-location>/WEB-INF/tld/c.tld</j2ee:taglib-location>
</j2ee:taglib>
</j2ee:jsp-config>



</j2ee:web-app>


The welcome file listed in the web.xml is displaying fine. However, when I point my browser to a URL that I setup to map to a controller, I get a status 404. When I use the webapp2_3.dtd as "template", the view returned by the controller is rendered, however the taglibs are not interpreted properly.