java 如何在jsp中使用从spring mvc返回的Model对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27300722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Model object returned from spring mvc in jsp
提问by kumuda
Hi I am new to Spring mvc. I am using ModelAndViewas return object in one of my controllers. I am not understanding how to access the Model object returned to the jsp for display. This is my code :
嗨,我是 Spring mvc 的新手。我在我的一个控制器中使用 ModelAndViewas 返回对象。我不明白如何访问返回给 jsp 的 Model 对象进行显示。这是我的代码:
@Controller
public class GetSongServlet extends HttpServlet {
@RequestMapping(value = "/getSong", method = RequestMethod.GET)
public ModelAndView getSong(@RequestParam(value = "songTitle", required = false) String
title) {
MusicPlayerService service = MusicPlayerServiceImpl2.getInstance();
try {
Song song = service.getSong(title);
System.out.println(song.getSongId());
// request.setAttribute("song", song);
/*
* RequestDispatcher requestDispatcher = request
* .getRequestDispatcher("viewSong");
* requestDispatcher.forward(request, response);
*/
return new ModelAndView("viewsong", "song", song);
}
}
<input type="text" class="form-control focusedInput" id="title"
value=<%="${song.title}"%> name="title" disabled />
<input type="text" class="form-control focusedInput" id="album"
value=<%= "${song.album.albumName}"%> name="album" disabled />
<input type="text" class="form-control focusedInput" id="artist"
value=<%= "${song.artist}"%> name="artist" disabled />.........
public class Song{
private int songId;
private Album album;
private String title;
private int rating;
private String artist;
private String composer;
private Genre genre;
}
回答by Sotirios Delimanolis
You don't access the model through the JSP. Model
(and all corresponding types: ModelAndView
, ModelMap
, etc.) is a Spring abstraction around HttpServletRequest
attributes.
您不通过 JSP 访问模型。Model
(以及所有相应的类型:ModelAndView
,,ModelMap
等)是围绕HttpServletRequest
属性的 Spring 抽象。
When Spring has finished invoking your handler method, it will move all model attributes it has collected to the HttpServletRequest
attributes.
当 Spring 完成调用您的处理程序方法时,它将把它收集到的所有模型属性移动到这些HttpServletRequest
属性中。
You can access these in JSP with the EL expression
您可以使用 EL 表达式在 JSP 中访问这些
${some.attribute}
but don't try to put EL within a scriptlet like you do here
但不要像这里那样尝试将 EL 放在 scriptlet 中
<%= "${song.artist}"%>
回答by Junior Ales
In order to your JSP page be able to display the property of your object you need to implement a public getter of it.
为了您的 JSP 页面能够显示您的对象的属性,您需要实现它的公共 getter。
In your case, your class Song
has an attribute title
but as it's a private attribute you gonna need a public method called getTitle()
returning the title
attribute. Your JSP will be able to render title
if you write something like that:
在你的情况下,你的类Song
有一个属性,title
但因为它是一个私有属性,你需要一个称为getTitle()
返回title
属性的公共方法。title
如果您编写如下内容,您的 JSP 将能够呈现:
<input type="text" value="${song.title}" name="title" disabled />