- springmvc freemarker 动态加载(不用重启tomcat立即生效)
- FreeMarker 判断List不为空
- Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
- Java模板引擎 FreeMarker介绍
- FreeMarker入门教程
- FreeMarker基本标签的使用
- FreeMarker基本数据类型
- FreeMarker集合(List、Map)
- FreeMarker自定义指令
- FreeMarker四种变量的用法
- FreeMarker模板包含及名称空间
- freemarker 空值处理
知识点
相关文章
更多最近更新
更多Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
2019-03-06 11:43|来源: 网路
在使用springmvc+freemarker,有可能你会遇到以下异常(如:资源找不到重定向到404的请求时):
javax.servlet.ServletException: Cannot expose request attribute 'website' because of an existing model object of the same name
at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:123)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
看一下源码,可以发现如果exposeRequestAttributes设为true,model中已经存在该key,且allowRequestOverride为false是,就会报该错。session一样:
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { String attribute; Object attributeValue; if(this.exposeRequestAttributes) { for(Enumeration en = request.getAttributeNames(); en.hasMoreElements(); model.put(attribute, attributeValue)) { attribute = (String)en.nextElement(); if(model.containsKey(attribute) && !this.allowRequestOverride) { throw new ServletException("Cannot expose request attribute '" + attribute + "' because of an existing model object of the same name"); } attributeValue = request.getAttribute(attribute); if(this.logger.isDebugEnabled()) { this.logger.debug("Exposing request attribute '" + attribute + "' with value [" + attributeValue + "] to model"); } } } if(this.exposeSessionAttributes) { HttpSession session = request.getSession(false); Object attributeValue; String attribute; if(session != null) { for(Enumeration en = session.getAttributeNames(); en.hasMoreElements(); model.put(attribute, attributeValue)) { attribute = (String)en.nextElement(); if(model.containsKey(attribute) && !this.allowSessionOverride) { throw new ServletException("Cannot expose session attribute '" + attribute + "' because of an existing model object of the same name"); } attributeValue = session.getAttribute(attribute); if(this.logger.isDebugEnabled()) { this.logger.debug("Exposing session attribute '" + attribute + "' with value [" + attributeValue + "] to model"); } } } } if(this.exposeSpringMacroHelpers) { if(model.containsKey("springMacroRequestContext")) { throw new ServletException("Cannot expose bind macro helper 'springMacroRequestContext' because of an existing model object of the same name"); } model.put("springMacroRequestContext", new RequestContext(request, response, this.getServletContext(), model)); } this.applyContentType(response); this.renderMergedTemplateModel(model, request, response); }
解决方法可以把allowRequestOverride 设为 true,下面是freemarker视图的配置:
<!-- 配置freeMarker视图解析器 --> <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=UTF-8"/> <property name="exposeRequestAttributes" value="true"/> <property name="exposeSessionAttributes" value="true"/> <property name="exposeSpringMacroHelpers" value="true"/> <property name="allowSessionOverride" value="true" /> <property name="allowRequestOverride" value="true" /> <property name="cache" value="true"/> <property name="suffix" value=".ftl"/> <property name="order" value="0"/> </bean>
相关问答
更多-
JavaScript - 跨源请求被阻止 - 非服务器解决方案(JavaScript - cross origin request blocked - non-server solution)[2023-03-22]
用你提供的代码试图完成的事情有几个问题。 首先,您试图向未托管在http服务器上的资源发出Ajax请求。 Ajax是XMLHttpRequest的一个包装器,用于使用http协议获取资源。 但是,它可以支持其他协议,例如文件和ftp。 其次,CORS不受浏览器控制,它受http服务器控制。 跨域原始请求可以正常工作,但前提是您请求的资源使用允许您的域访问它的http标头进行响应。 由于您请求的资源与http无关,因此可能会引发错误。 那么为什么图像使用file://方案? 标签支持使用浏览器支持 ... -
似乎有三种情况: status为无 status is not None ,但status.string是 status和status.string是not None 您必须提供处理每个案例的代码。 if status is not None and status.string is not None: st = status.string[:-3] else: st = "Not listed" database.append({ "Title": title[0] ...
-
您正在使用表单中的 ColorField 模型 ,但这些是两个完全不同的上下文。 看起来您还试图将常规表单用作ModelForm 。 ModelForm简介 。 将表单更改为如下所示: class EventForm(forms.ModelForm): class Meta: model = Event fields = ['name', 'beggining_time', 'ending_time', 'color'] 这将在渲染时自动使用ColorWidget ...
-
您无法直接从视图返回实例,您需要返回响应。 通常是通过渲染模板: return render(request, 'mytemplate.html', {'form': form}) 虽然可以想象你可以返回包含在HttpResponse中的表单本身: return HttpResponse(form) 但它看起来很可怕。 (另外你应该考虑调用它们是什么;模型实例不是一种形式。) You can't return an instance directly from a view, you need to r ...
-
如果您想要自行托管服务而不是在IIS中托管服务,或者由于某种原因您希望将服务接口作为程序集共享,则将它放在单独的程序集中可能会更容易。 我建议为服务接口(包括数据合同等)推荐一个程序集,一个用于服务实现,另一个(如果需要)用于自主机(例如Windows NT服务或命令行应用程序)。 这是最干净的分离,并没有真正的理由不去那样,恕我直言。 渣子 IF you ever want to self-host the service instead of hosting it inside IIS, or if y ...
-
据我了解, map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getPrincipal(session)); 从会话中提取user属性并将其放入模型中。 @SessionAttriubtes指示Spring自动执行相同操作。 因此他们发生冲突 您应该只使用其中一种方法,而不是同时使用这两种方法。 As far as I understand, map.addAttribute(org.brahmaa.ads.util.impl.UserInfo.getP ...
-
做就是了 this.model.set({"rank": val}); 代替 this.options.model.set({"rank": val}); 视图中的模型通过this.model而不是this.options.model访问 Just do this.model.set({"rank": val}); instead of this.options.model.set({"rank": val}); The model within a view is accessed via thi ...
-
我也会注意到(我确定你是)CoreData即将到来,并确保你为未来做出正确的选择。 I'd also be aware (as I'm sure you are) that CoreData is just around the corner, and make sure you make the right choice for the future.
-
我发现你的代码没问题,只要你在这里给出。 我用你的代码模仿了这种情况,但不幸地发现没有异常 。 你可能做错了的事情是你正在tomcat中运行一些旧的构建代码。 尝试清理构建并重新部署到您的容器中。 注意:一个友好的建议。 你做错了一件事就是将你的表单操作/register/process发送给容器根目录(例如localhost:8080/register/process )。 你会得到404 。 你不可能想要那样。 register/process应该是你的URL,并且这会将请求发送给你的应用程序上下文。 ...
-
显然,仅当TrackUser链接到另一个模型时才会出现问题。 在其中一个模型中,我更改了__unicode__ def __unicode__(self): return self.user 至 def __unicode__(self): return self.user.get_full_name() 这解决了这个问题 Apparently the problem only appeared when a TrackUser was linked to another model. ...