我知道這已經被討論了很多,但在嘗試所有我發現後,它仍然不起作用:下面是我的代碼(我知道,現在我將所有內容都保存在控制器中)但它應該仍然有效,我想。我正在使用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.
你有沒有試圖改變'$ {model.hmn.FirstName}''到$ {hmn.FirstName}'? –
是因爲'person'的'FirstName'尚未初始化?在'GetName()'中,'FirstName'通過給它直接賦值'「Johnny」而不是通過setFirstName(「Johnny」)來設置。 –
不幸的是,雖然在方法GetName()的屬性中設置了名字。除非不允許將屬性與方法分開?沒有意義。你有沒有想要分享的例子?謝謝! – Keltanis