0

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

雇员.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。

4

6 回答 6

2

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

@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;
    }
于 2013-02-07T00:22:11.040 回答
1

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

于 2013-02-07T00:32:47.617 回答
0

如果这些作品都没有,只需使用

java中的request.setParameter

jsp中的response.getParameter

于 2013-02-07T05:39:42.107 回答
0

您应该将其添加到您的页面标题中:

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

或者直接使用EL:

 <h1>${model.name}</h1>
于 2014-08-30T15:42:48.200 回答
0

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

于 2017-04-04T07:24:30.487 回答
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>
于 2019-05-25T06:11:34.427 回答