ModelMap没有将bean传递给jsp

时间:2013-03-28 12:07:10

标签: java jsp spring-mvc

我的app有问题。我使用SpringMVC,Maven,Hibernate。只是我试图在jsp视图中打印一些数据。
我的web.xml文件:

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

  <display-name>app</display-name> 
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

我的控制员的一部分:

@RequestMapping(value="/", method=RequestMethod.GET)
public String generateMainPage(ModelMap model)
{
    UserModel um = new UserModel();
    // setting UserModel data which are 100% not null
    String simpleString = "hello "+new Date().toString();
    int simpleNumber = 1234;
    model.addAttribute("userModel", um);
    model.addAttribute("string", simpleString);
    model.addAttribute("number", simpleNumber);
    return "mainPage";
}

我的模特:

public class UserModel
{
    private String name;
    private String age;
    // setters and getters
}

现在,问题是我的jsp没有从bean返回值 ONLY 。我发现了一个类似的问题HERE,但它没有解决任何问题。

我的部分jsp&#34;主页&#34;页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page</title>
</head>
<body>
     ${simpleString} <br/> <!-- this works fine -->
     ${simpleNumber} <br/> <!-- this works fine also -->
     ${um.name} <!-- this return nothing -->
     ${um.age} <!-- same here -->
</body>
</html>

1 个答案:

答案 0 :(得分:1)

${um.name} <!-- this return nothing -->

应该是

${userModel.name}

因为在控制器中你这样做

model.addAttribute("userModel", um);

字符串是属性的名称,它可以是您想要的任何内容