我有以下类发送 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。