jsp不显示变量的值

时间:2013-02-07 00:15:36

标签: spring jsp spring-mvc

我有以下类发送name变量的值,但jsp没有显示它。 它只显示hello world消息

Employee.java

public class Employee {

    private String name;

    public Employee(){
       this.name = "Daniel";
    }

    public String getName() {
        return name;
    }

    public void setName(String name)
    {
       this.name = name;
    }

Emp.java

public class Emp implements Controller {


private Employee empp;

protected final Log logger = LogFactory.getLog(getClass());

   @Override
     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        System.out.println("empp:"+this.empp.getName());
        String myname = empp.getName();
        logger.info("Returning hello view");

    return new ModelAndView("emp.jsp","name",myname);  

     }

Emp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <h2><c:out value="${name}"/></h2>
    </body>
</html>

还使用了以下

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    System.out.println("empp:"+this.empp.getName());
    String myname = empp.getName();
    logger.info("Returning hello view");
    Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("name", this.empp.getName());

return new ModelAndView("emp.jsp","model",myModel);
}

Emp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <h2><c:out value="${model.name}"/></h2>
    </body>
</html>

我用过

<h1><%= pageContext.findAttribute("model.name") %></h1>

但它返回Null。

6 个答案:

答案 0 :(得分:2)

您需要将employeeInstance.getName()的值添加到ModelAndView。根据{{​​3}},这是如何做到的:

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", employeeInstance.getName());
        modelAndView.setViewName("Emp.jsp");
        return modelAndView;
    }

答案 1 :(得分:1)

作为控制器的接口,它们的变量不能改变,它只是一个只读值。是接口控制器中的变量名吗?

答案 2 :(得分:0)

如果这些都不能正常使用

java中的request.setParameter

jsp中的response.getParameter

答案 3 :(得分:0)

你应该把它添加到你的页面头:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

或直接使用EL:

 <h1>${model.name}</h1>

答案 4 :(得分:0)

我的错误是导入了org.springframework.web.portlet.ModelAndView而不是org.springframework.web.servlet.ModelAndView。使用后者解决了我的问题。

答案 5 :(得分:0)

在您的jsp中进行小的修改

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <h2>/${name.getName()}</h2>
    </body>
</html>