Spring Web MVC:简单控制器 - $-表达式不起作用

发布于 2024-10-22 04:41:14 字数 3677 浏览 2 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-10-29 04:41:14

您的 web.xml 缺少正确的标头,它应该类似于:

<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"> 

如果您将其省略,并且只有一个裸露的 ,则容器将默认为旧的、EL 之前的 JSP 版本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文