Reputation: 772
I have following class that send the value of name variable but the jsp does not show it. It just show the hello world message
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>
Also used the following
@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>
I used
<h1><%= pageContext.findAttribute("model.name") %></h1>
but it returns Null.
Upvotes: 0
Views: 4606
Reputation: 1
Make this small modification in your 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>
Upvotes: 0
Reputation: 2075
The mistake in my case was I imported org.springframework.web.portlet.ModelAndView
instead of org.springframework.web.servlet.ModelAndView
. Using the latter solved my problem.
Upvotes: 0
Reputation: 1120
you should add this into your page head:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Or directly use EL:
<h1>${model.name}</h1>
Upvotes: 0
Reputation: 57
If none of these works just use
request.setParameter in java
and
response.getParameter in jsp
Upvotes: 0
Reputation: 574
Being controller an interface, their contant variables can't change, it's just a read-only value. Is the variable name in the Interface Controller?
Upvotes: 1
Reputation: 34657
You need to add the value of employeeInstance.getName()
to ModelAndView. According to the docs, here's how to do so:
@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;
}
Upvotes: 2