Spring 模型未应用
Spring model isn't applied
我有以下 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>
这不是 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>
.....
我有以下 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>
这不是 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>
.....