I'm starting with Spring Web MVC. I have very simple controller and view, but when i open page
http://localhost:8080/springonly/spring/hello i get: ${message}
, not Hello World
Anybody开发者_StackOverflow中文版 know what is wrong with that ? Why $
-expression have not been recognized ?
View - hello.jsp
:
${message}
Controller - HelloController.java
:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("hello", "message", "Hello World");
}
}
web.xml
:
<web-app>
<display-name>Spring Only WebApp</display-name>
<servlet>
<servlet-name>springonly</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springonly</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
:
<!-- scan for Controllers -->
<context:component-scan base-package="net.marioosh.spring.springonly" />
<!-- view resolver -->
<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/pages/" />
<property name="suffix" value=".jsp" />
</bean>
log say:
2011-03-15 13:43:12,703 DEBUG [DispatcherServlet:693] : DispatcherServlet with name 'springonly' processing GET request for [/springonly/spring/hello]
2011-03-15 13:43:12,718 DEBUG [DefaultAnnotationHandlerMapping:221] : Mapping [/hello] to HandlerExecutionChain with handler [net.marioosh.spring.springonly.HelloController@120b2da] and 1 interceptor
2011-03-15 13:43:12,718 DEBUG [DispatcherServlet:769] : Last-Modified value for [/springonly/spring/hello] is: -1
2011-03-15 13:43:12,718 DEBUG [HandlerMethodInvoker:173] : Invoking request handler method: public org.springframework.web.servlet.ModelAndView net.marioosh.spring.springonly.HelloController.hello(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
2011-03-15 13:43:12,718 DEBUG [DispatcherServlet:1045] : Rendering view [org.springframework.web.servlet.view.JstlView: name 'hello'; URL [/WEB-INF/pages/hello.jsp]] in DispatcherServlet with name 'springonly'
2011-03-15 13:43:12,718 DEBUG [JstlView:328] : Added model object 'message' of type [java.lang.String] to request in view with name 'hello'
2011-03-15 13:43:12,718 DEBUG [JstlView:236] : Forwarding to resource [/WEB-INF/pages/hello.jsp] in InternalResourceView 'hello'
2011-03-15 13:43:12,718 DEBUG [DispatcherServlet:674] : Successfully completed request
Your web.xml
is missing the correct header, it should be something like:
<web-app
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"
version="2.4">
If you leave that out, and just have a bare <web-app>
, the container will default to an old, pre-EL version of JSP.
精彩评论