Spring Web MVC:简单控制器 - $-表达式不起作用
我从 Spring Web MVC 开始。我有非常简单的控制器和视图,但是当我打开页面时 http://localhost:8080/springonly/spring/hello 我得到: ${ message}
,而不是 Hello World
有人知道这有什么问题吗?为什么$
-表达式未被识别?
视图 - hello.jsp
:
${message}
控制器 - 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>
日志说:
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
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
web.xml
缺少正确的标头,它应该类似于:如果您将其省略,并且只有一个裸露的
,则容器将默认为旧的、EL 之前的 JSP 版本。Your
web.xml
is missing the correct header, it should be something like:If you leave that out, and just have a bare
<web-app>
, the container will default to an old, pre-EL version of JSP.