0

I know this has been talked a lot about but, after trying all I find, it still does not work: Below is my code (I know, right now I am keeping everything in the controller) but It should still work, I think. I am using Spring STS, Maven build.

import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HomeController {
public String FirstName;
public String LastName;

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView init() {

    Map<Object, Object> myModel = new HashMap<Object,Object>();
    myModel.put("hmn", this.GetName());

    return new ModelAndView("home","model",myModel);
}

public class Person{

    private String FirstName;

    public String getFirstName() {
        return FirstName;
    };   

    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    };  

};
public Person GetName(){
    Person person = new Person();
    person.FirstName = "Johnny";
    return person;
}
}

I can return the ${model.hmn} in jsp , but I want to return ${model.hmn.FirstName} as the Person model will have more than just the first name...Thoughts?

EDIT: Okay, So I tried the suggestion below. I did not return anything back on the page.

Setting everything back to the way is way. Here's the error I received:

HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 52

--------------------------------------------------------------------------------

type Exception report

message An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 52

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-      INF/views/home.jsp at line 52

49:         </div>
50:         <div id="Content">
51:             <div id="BodyContent">
52:                 <c:out value="${model.hmn.FirstName}"/>
53:             </div>
54: 
55:         </div>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause 

javax.el.PropertyNotFoundException: Property 'FirstName' not found on type com.oxstudios.spring.HomeController$Person
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
javax.el.BeanELResolver.property(BeanELResolver.java:353)
javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
org.apache.jsp.WEB_002dINF.views.home_jsp._jspx_meth_c_005fout_005f0(home_jsp.java:253)
org.apache.jsp.WEB_002dINF.views.home_jsp._jspService(home_jsp.java:131)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.
Keltanis
  • 124
  • 1
  • 1
  • 9
  • Have you tried changing `${model.hmn.FirstName}` to `${hmn.FirstName}`? – S.C. Jul 22 '14 at 05:14
  • Is it because the `FirstName` of `person` is not yet initialized? In `GetName()`, `FirstName` is set by directly assigning `"Johnny"` to it rather than through the setter `setFirstName("Johnny")`. – S.C. Jul 23 '14 at 02:33
  • Unfortunately not The first name is being set though in the properties above the method GetName(). Unless properties are not allowed to be separate from methods? Makes no sense. Do you have an example you would like to share? Thanks! – Keltanis Jul 23 '14 at 23:33
  • I have a working simple project at hand. The only difference lies within the `init()` method. My function signature is `public void init(Model model)`, and I added a reference to my class instance by `model.addAttribute("info", new MyInfo());`. In my JSP, I can access fields in my `MyInfo` class with `${info.firstName}`, `${info.city}` etc. – S.C. Jul 24 '14 at 03:02
  • hmmm...Not getting errors but just coming up blank. Are you able to supply the working code (with 'Dummy Data')? – Keltanis Jul 24 '14 at 12:03
  • Are you facing the same problem as [this one](http://stackoverflow.com/questions/1529184/jsps-not-displaying-objects-from-model-in-spring)? – S.C. Jul 25 '14 at 01:49
  • Which one? I think I'm about to scrap Spring MVC at this point. – Keltanis Jul 25 '14 at 03:01
  • [JSPs not displaying objects from model in Spring](http://stackoverflow.com/questions/1529184/jsps-not-displaying-objects-from-model-in-spring) – S.C. Jul 25 '14 at 03:28
  • Thanks fro all the help, but it looks like this is not going anywhere. I will leave this unanswered for now. – Keltanis Jul 25 '14 at 12:19

1 Answers1

1

The problem is cause by the way you are accessing your property in jsp,

${model.hmn.FirstName}

should instead be

${model.hmn.firstName}

What happens in behind the scenes is that BeanELResolver from the JSP picks up capitalized 'FirstName' as the key for the property. On the other hand, the Inspector class that inspects the bean (Person class) creates a map based on the accessor methods, but the keys are built by stripping the accessor prefixes (get, set, is) and lower-casing the first letter. BeanELResolver ends up searching for the property with the key 'FirstName' ia a map that holds the property under the key 'firstName'.

All this follows the java beans specification

Master Slave
  • 27,771
  • 4
  • 57
  • 55