不应用Spring模型

时间:2015-01-02 11:11:03

标签: java spring jsp servlets web.xml

我有以下jsp标记:

<p>The name of the workplace is ${w}</p>

控制器:

@Controller
public class HelloController{

    @Autowired
    private WorkplacementDao workplacementDao;

    @RequestMapping("/hello")
    public String hello(ModelMap m){
        String workplaceName = workplacementDao.getNameById(2);
        m.addAttribute("w", workplaceName);
        return "hello";
    }
   //...
}

dispatcher-servlet.xml如下:

<context:component-scan base-package="com.mycompany.maventestwebapp" />

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

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/introDB"/>
</bean>

<bean id="workplacementDao" class="com.mycompany.maventestwebapp.db.dao.WorkplacementDAOImpl">
    <property name="dataSource" ref="dataSource" />
</bean>

现在,当我在浏览器中请求/hello页面时,我得到:

The name of the workplace is ${w}

${w}未被替换为实际值。为什么?怎么了?

UPD:web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:1)

这不是Spring的问题。正如评论中所建议的那样,您的表达式未被评估,因为您的web.xml没有指定支持该功能的现代版本的Servlet / JSP API。

将您的web.xml更改为:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<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">

    <servlet>
    .....