Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在寻找一种简洁的方法,通过控制器';s模型对象_Java_Spring_Spring Mvc - Fatal编程技术网

Java 正在寻找一种简洁的方法,通过控制器';s模型对象

Java 正在寻找一种简洁的方法,通过控制器';s模型对象,java,spring,spring-mvc,Java,Spring,Spring Mvc,我的JSP中有一个标记,它是使用请求处理程序中的值设置的: <title><c:out value="${title}"/></title> 如果您想从控制器中找出addTitle方法,也许可以将它们放在HandlerInterceptor实现中 可能是这样的: public class TitleInterceptor implements HandlerInterceptor { public void postHandle(HttpServle

我的JSP中有一个
标记,它是使用请求处理程序中的值设置的:

<title><c:out value="${title}"/></title>

如果您想从控制器中找出addTitle方法,也许可以将它们放在HandlerInterceptor实现中

可能是这样的:

public class TitleInterceptor implements HandlerInterceptor {
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
           String requestUrl = (String)request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); 
           String title = "";
           if ("/url1.htm".equals(requestUrl)) {
              title = "Title 1";
           } else if ("/url2.htm".equals(requestUrl)) {
              title = "Title 2";
           }
           modelAndView.getModel().put("title", title)
        }

}
如果您需要一些处理来确定标题,那么拦截器可用的modelAndView可能会包含有助于确定给定url的标题的数据。如果不需要处理,只需简单地将标题映射到url,您甚至可以在applicationContext.xml中的bean配置期间将其实现为可配置映射

我发现一些有助于实现HandlerInterceptor的链接可以在这里找到:
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/HandlerInterceptor.html

如果您不想沿着
拦截器
方面
的道路前进(将所有东西都放在控制器中):

  • 创建一个所有控制器扩展的
    BaseController
  • BaseController
    中将URL映射到标题中,使用
    HashMap
  • addTitle
    方法也放在那里,修改以返回与JSP名称相同的字符串
BaseController
code:

public ModelMap addTitle(ModelMap model, String page) {
   model.addAttribute("title", titleMap.get(page));
   return page;
}
控制器代码变为:

@RequestMapping(value = "/goo", method = RequestMethod.GET)
public final String goo(final ModelMap model) {
   return super.addTitle(model, "goo");
}

${title}
执行与JSP中的
完全相同的操作。很抱歉,我没有回答你真正的问题。这完全不一样,c:out默认转义html
@RequestMapping(value = "/goo", method = RequestMethod.GET)
public final String goo(final ModelMap model) {
   return super.addTitle(model, "goo");
}