java - spring web mvc配置问题

标签 java spring spring-mvc model-view-controller

我不知道我错过了什么,我想做的就是使用spring来捕获重定向并调用方法列表,这样我就可以在listentries.jsp页面上看到消息“message”:

这是我的 web.xml:

<web-<app>
<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>    
</servlet>

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

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

这是上下文配置文件 applicationContext,包含 viewresolver:

<beans>
<context:component-scan base-package="net.tirasa.springaddressbook"/>
<bean id="dao" class="net.tirasa.springaddressbook.SpringEntryDAO">
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost/Rubrica" />
  <property name="username" value="matt3o" />
  <property name="password" value="secret" />
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  <property name="prefix" value="/"/>
  <property name="suffix" value=".jsp"/>
</bean>
</beans>

这是 AddressBookController 类:

@Controller
@RequestMapping(method = GET, value = "/springaddressbook")
public class AddressBookController {
    @RequestMapping(value = "/listentries")
    public ModelAndView list() {
        String message = "hello";
        return new ModelAndView("listentries", "message", message);
    }
}

并遵循以下观点: 索引.jsp

<html>
 <head><title>Index</title></head>
  <body>
   <h3>Rubrica:</h3>
   <br/>
   <a href="listentries.jsp"> List Entries</a><br/>
 </body>

和listentries.jsp

<html>
 <head><title>List Entries</title></head>
  <body>
   <h3>List entries</h3>
    ${message}
   </body>
</html>

最佳答案

EL 是在 Servlet 2.4 版本中引入的。 将此代码附加到 web.xml 的顶部以获取 EL 变量值,

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

关于java - spring web mvc配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743517/

相关文章:

java - 在命令 dspring.config.loc 之后覆盖 application.property 文件

java - 如何在Spring Controller 中访问applicationContext.xml?

java - 多部分解析器未处理多部分请求

Java- Jersey ,JAX RS

java - 使用 Swing 从监听器添加组合框值

java - Spring MVC 表单 - 下拉列表中没有项目?

java - 如何使用 Hibernate 和 Spring Boot 启用批量插入

java - Spring中抛出404并重定向到自定义错误页面

java - 跨不同 JVM 的相同 SparkContext

java - Image.getGraphics() 返回 null