2014-07-22 153 views
0

我知道這已經被討論了很多,但在嘗試所有我發現後,它仍然不起作用:下面是我的代碼(我知道,現在我將所有內容都保存在控制器中)但它應該仍然有效,我想。我正在使用Spring STS,Maven構建。JSP頁面的Spring MVC映射信息

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; 
} 
} 

我可以返回JSP中$ {} model.hmn,但我想回到$ {} model.hmn.FirstName隨着人車型將擁有的不僅僅是名字......思考更多?編號: 好的,所以我嘗試了下面的建議。我沒有在頁面上返回任何東西。

設置一切都回到了路上。這是我收到的錯誤:

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. 
+0

你有沒有試圖改變'$ {model.hmn.FirstName}''到$ {hmn.FirstName}'? –

+0

是因爲'person'的'FirstName'尚未初始化?在'GetName()'中,'FirstName'通過給它直接賦值'「Johnny」而不是通過setFirstName(「Johnny」)來設置。 –

+0

不幸的是,雖然在方法GetName()的屬性中設置了名字。除非不允許將屬性與方法分開?沒有意義。你有沒有想要分享的例子?謝謝! – Keltanis

回答

1

的問題是,你正在訪問JSP中你的財產的方式的原因,

${model.hmn.FirstName} 

應改爲

${model.hmn.firstName} 

會發生什麼事背後的場景是來自JSP的BeanELResolver獲取大寫的'FirstName'作爲屬性的關鍵字。另一方面,檢查Bean(Person類)的Inspector類基於訪問器方法創建映射,但是通過刪除訪問器前綴(get,set,is)並將第一個字母的下方括起來構建鍵。 BeanELResolver最終以關鍵字'FirstName'ia一個包含關鍵字'firstName'下的屬性的映射來搜索屬性。

這一切都遵循java beans specification