1

Here is my controller:

@RequestMapping("/com/index.do")
public String index(ModelMap model) throws Exception {
    MyClass obj=new MyClass();
    model.addAttribute("obj",obj);
    return "/com/index";
}

Here is my view:

<%@page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%@page import="org.springframework.ui.ModelMap" %>

<%
    ModelMap model=new ModelMap();
    Object obj=model.get("obj");
%>

In here obj is null. In this case, how to retrieve that obj in view?

Note: I need to use in jsp tag (<% %>), not like this:

${obj}

Thanks!

RAS
  • 8,100
  • 16
  • 64
  • 86
Leap Bun
  • 2,265
  • 5
  • 28
  • 44

2 Answers2

2

You need to access using the Expression Language in JSP like ${obj}. For example if you want to print that in your jsp in an input text

(assuming your MyClass is having getName and setName or you can change whatever getter setter you have in that class)

If you using spring tld's then you check out spring:form tags to set the path so that your bean and the html elements can be binded.

Check this out http://forum.springsource.org/showthread.php?73583-Accessing-model-attributes-in-JSP

raddykrish
  • 1,866
  • 13
  • 15
  • So no way to use in jsp tag (<% %>)? – Leap Bun Mar 14 '12 at 07:58
  • Its bad practice to use scriptlets in the jsp code. See related posts here http://stackoverflow.com/questions/6399666/in-a-jsp-scriptlet-how-do-you-access-a-java-util-date-value-passed-from-a-sprin Its always good to use EL to access these attributes. – raddykrish Mar 14 '12 at 14:55
  • I need to access method from that object, that's why I use scriptlet. – Leap Bun Mar 15 '12 at 02:48
  • You can still access that using EL. For example your class MyClass is having a method called getName(), definitely you would have declared a class level attribute in that MyClass as String name, you can access that in EL like "{obj.name}", internally that binding will happen and you will get the result of calling the method. For example you are bringing a value "Leap Bun" in that attribute you jsp will display "Leap Bun" when rendered. – raddykrish Mar 15 '12 at 03:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8900/discussion-between-leap-bun-and-raddykrish) – Leap Bun Mar 15 '12 at 06:44
1

you must import jstl lib in jsp page

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