I’m trying to get my spring application working, following a guy who posted an example with earlier spring version. I’ve been having many problems with his example so I’m not posting a link so as not to lead anyone else astray. Fighting through issues, I was able to get to the point where things are working. Somewhat. I have not been able to figure out the following two things:
$(message)isn’t printing the message which is a bean of type String.<c:out value="${message}"prints the message but only if I add<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>to the jsp. I would assume that adding jtslview class to the resolver would be enough. It’s not. What’s wrong here?
setup
Eclipse IDE for Java EE Developers 1.4.1.20110909-1818 epp.package.jee
Tomcat 7
Spring 3 distribution (all jars in /lib)
lib/jstl-1.2.jar
jsp in question
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Message View</title>
</head>
<body>
$(message)
<br>
message=$(message);
<br>
message=<c:out value="${message}" />
</body>
</html>
this prints the following. The message “bean” is clearly passed to the page.
$(message)
message=$(message);
message=Hello World from Spring MVC!
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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"
id="WebApp_ID"
version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
<context:component-scan base-package="com.helloworldexample.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
controller (just in case)
package com.helloworldexample.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MesssageController {
@RequestMapping("/hello")
public ModelAndView handleHello() {
String message="Hello World from Spring MVC!";
return new ModelAndView("messageView", "message", message);
}
@RequestMapping("/welcome.html")
public ModelAndView handleWelcome() {
String message="Welcome in Spring MVC!";
return new ModelAndView("messageView", "message", message);
}
}
The syntax for the JSP expression language is
${some expression}. Not$(some expression).Each time you use a JSP tag, its tag library must be declared in the JSP. The fact that you configure Spring to use a JstlView has nothing todo with the possibility of using the
<c>tags. It just configures Spring to perform appropriate actions before dispatching to the view.<c:out>should always be used to render a string, unless you’re absolutely sure that the string doesn’t contain any character that must be HTML-escaped. Not using it opens the door to attacks where a user could submit a text containing</html>or a piece of javascript code that would compromise your web site.Read the official tutorial about servlets and JSPs: http://docs.oracle.com/javaee/5/tutorial/doc/bnadp.html