Java、Spring、Apache Tiles 错误:无法解析名称为“联系人”的视图;在名为“spring”的 servlet 中
控制器:
@Controller
@SessionAttributes
public class ContactController {
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstName() +
"Last Name:" + contact.getLastName());
return "redirect:contacts.html";
}
@RequestMapping("/contact")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}
}
这是我的tiles.xml:
<tiles-definitions>
<definition name="base.definition"
template="/WEB-INF/jsp/layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
</definition>
<definition name="contact" extends="base.definition">
<put-attribute name="title" value="Contact Manager" />
<put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
</definition>
错误是这样的:
org.apache.jasper.JasperException: javax.servlet.ServletException: Could not resolve view with name 'contact' in servlet with name 'spring'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
随时询问更多源代码
controller:
@Controller
@SessionAttributes
public class ContactController {
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {
System.out.println("First Name:" + contact.getFirstName() +
"Last Name:" + contact.getLastName());
return "redirect:contacts.html";
}
@RequestMapping("/contact")
public ModelAndView showContacts() {
return new ModelAndView("contact", "command", new Contact());
}
}
this is my tiles.xml:
<tiles-definitions>
<definition name="base.definition"
template="/WEB-INF/jsp/layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
</definition>
<definition name="contact" extends="base.definition">
<put-attribute name="title" value="Contact Manager" />
<put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
</definition>
error is this:
org.apache.jasper.JasperException: javax.servlet.ServletException: Could not resolve view with name 'contact' in servlet with name 'spring'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
feel free to ask for any more source code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的最后一个问题中,您表明您使用了带有前缀和后缀的 UrlBasedView 解析器。
删除该前缀和后缀。
这应该是enogth:
顺便说一句:此配置允许两个tzps(只有语义不同)的图块配置文件
/WEB-INF/layouts/layouts.xml
包含所有(我们称之为)“基本定义”/WEB-INF/views/**/views.xml
包含扩展“基本定义”的具体定义“(例如“联系人”定义) - 您可以为每个文件夹拥有一个单独的view.xml
- 如果您对视图进行分组,例如所有视图(创建、更新、显示和列表),这非常有用每个实体位于单独的文件夹中如果您不需要此功能,则只需删除该行即可。
In your last question you showed that you used a UrlBasedView Resolver with an pre and suffix.
Remove that pre and suffix.
That should be enogth:
btw: this configruation allowes two tzps (only there sermatic differs) of tiles configuration files
/WEB-INF/layouts/layouts.xml
contains all the (lets call it) "base definitions"/WEB-INF/views/**/views.xml
contains the concrete definitions that extends the "base definitions" (for example the "contact" definition) -- You can have a seperateview.xml
for each folder -- this is usefull if you group your views, for example all views (create, update, show and list) for each entity, in a separete folderIf you do not need this feature, then simply remove that line.