Spring MVC:Jsp文件不显示来自控制器的数据

时间:2015-10-08 10:03:21

标签: spring spring-mvc

这是我的控制器代码

    @RequestMapping(value="/testMvc")
        public String testero(Model model ){ 
                        String nameClient="HENOCKE";
                    Long CodeClient=2L;
                    String Address="LONDO Street";  

        System.out.println("****End and outputting to Jsp page: ****");
        return "rs";

        }

你可以看到我想向我的jsp文件显示数据,这是我的jsp文件:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" charset=UTF-8>
<title>Banque </title>
<link rel="stylesheet" type="text/css"
    href="<%=request.getContextPath()%>/resources/CSS/style1.css">

</head>
<body>
    <h2>Date Loaded from My controller....</h2>

    <table>
    <tr>
    <th>Name : </th> <th><c:out  value="${nameClient}"/></th>
    </tr>

    <tr>
    <th>id Client:  </th> <th> <c:out  value=" ${CodeClient}" /> </th>
    </tr>
    <tr>
    <th>Adress client: </th> <th> <c:out value="${Address}" /></th>
    </tr>

    </table>
</body>
</html>

如何将数据输出到我的jsp?这里 rs 是我的Jsp文件的名称

2 个答案:

答案 0 :(得分:4)

您需要将属性添加到模型中。

String nameClient = "Whatever";
model.addAttribute("nameClient",nameClient);

阅读本文:

http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html

答案 1 :(得分:0)

您应该创建ModelAndView对象以将值放入视图:

 @RequestMapping(value="/testMvc")
    public ModelAndView testero(){ 
       ModelAndView mov = new ModelAndView("rs");

       mov.addAttribute("nameClient", "HENOCKE");
       mov.addAttribute("CodeClient", 2L);
       mov.addAttribute("Address", "LONDO Street");

       System.out.println("****End and outputting to Jsp page: ****");
       return mov;
    }