如何在springmvc注入request中获取request对象

SpringMVC、Freemarker整合怎么获取request对象 - VC/MFC当前位置:& &&&SpringMVC、Freemarker整合怎么获取request对象SpringMVC、Freemarker整合怎么获取request对象&&网友分享于:&&浏览:12次SpringMVC、Freemarker整合如何获取request对象
SpringMVC与Freemarker整合需要用到request来获取locale,但是默认freemarker是获取不到request的,只能获取到Request,但是大写的Request与小写的request是不同的。大写的Request是freemarker.ext.servlet.HttpRequestHashModel的实例对象,而小些的request是HttpServletRequest对象,所以区别在于这里。那么我需要获取到小写的request对象需要怎么做了,请大家参看以下配置:&property name="viewResolvers"&&&&&&&&&&&& &list&&&&&&&&&&&&&&&& &!-- jsp&&&&&&&&&&&&&&& &bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&&&&&&&&&&&&&&&&&&&& &property name="prefix" value="/WEB-INF/views/" /&&&&&&&&&&&&&&&&&&&& &property name="suffix" value=".jsp" /&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&& --&&&&&&&&&&&&&&&& &!-- freemarker --&&&&&&&&&&&&&&&& &bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"&&&&&&&&&&&&&&&&&&&& &property name="cache" value="true"/&&&&&&&&&&&&&&&&&&&& &property name="prefix" value="/"/&&&&&&&&&&&&&&&&&&&&&&& &property name="suffix" value=".ftl"/&&&&&&&&&&&&&&&&&&&& &property name="exposeSpringMacroHelpers" value="true"/&&&&&&&&&&&&&&&&&&&& &property name="exposeRequestAttributes" value="true"/&&&&&&&&&&&&&&&&&&&& &property name="exposeSessionAttributes" value="true"/&&&&&&&&&&&&&&&&&&&& &property name="requestContextAttribute" value="request"/&&&&&&&&&&&&&&&&&&&& &property name="contentType" value="text/ charset=utf-8"/&&&& &&&&&&&&&&&&&&& &/bean&&&&&&&&&&&& &/list&&&&&&&& &/property&从里面的配置中我想大家应该都看到了 &propertyname="requestContextAttribute"value="request"&&/property&这一句就是把HttpServletRequest的属性存放到request这个变量里面的。一般我们要获取request.setAttribute(“key”,”value”)直接都可以使用Request["key"]来获取,而不必使用request。Freemarker里面使用request代码如下:${request.getlocale()}
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有Spring&mvc&3.2&下Ajax获取JSON对象问题&406错误
3.2.x通过@ResponseBody标签返回JSON数据的方法都报406错:&Failed
to load resource: the server responded with a status of 406 (Not
Acceptable)&以及报错描述:&The
resource identified by this request is only capable of generating
responses with characteristics not acceptable according to the
request "accept" headers ()&
Spring 3.2 配置如下:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"&
mediaTypes配置:
& atom=application/atom+xml
&html=text/html &
&json=application/json
用@ResponseBody返回对象出现问题,报406错误!!!
最后发现是:spring
3.2时requestedMediaTypes却为[text/html]的情况报406错误,还有一个原因可能是由于采用的后缀有关,如果使用*.htm,*.html等,默认就会采用[text/html]编码,若把AJAX请求URL后面加上*.json,*.shtml等就OK!
可参考:http://blog.csdn.net/gbtyy/article/details/
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。今天看啥 热点:
SpringMVC项目中获取所有URL到Controller Method的映射,springmvcmethod
Spring是一个很好很强大的开源框架,它就像是一个容器,为我们提供了各种Bean组件和服务。对于MVC这部分而言,它里面实现了从Url请求映射控制器方法的逻辑处理,在我们平时的开发工作中并不需要太多的理会这个Url是怎么和控制器中的方法建立映射的,一切都由Spring MVC帮我们搞定了。
但是我今天在做SDK工程的时候,突然产生一个想法:能否把我项目中的所有Url和Method的映射信息打印出来?以便我一眼就看出我已经完成了那些API接口开发,这些方法需要什么参数。就像下图所示:
private class RequestToMethodItem
public String controllerN
public String methodN
public String requestT
public String requestU
public Class&?&[] methodParmaT
public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName,
Class&?&[] methodParmaTypes)
this.requestUrl = requestU
this.requestType = requestT
this.controllerName = controllerN
this.methodName = requestMethodN
this.methodParmaTypes = methodParmaT
2、然后就是收集信息创建对象啦
@RequestMapping(value = &/index&, method = RequestMethod.GET)
public ModelAndView index(HttpServletRequest request)
ServletContext servletContext = request.getSession().getServletContext();
if (servletContext == null)
return null;
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//请求url和处理方法的映射
List&RequestToMethodItem& requestToMethodItemList = new ArrayList&RequestToMethodItem&();
//获取所有的RequestMapping
Map&String, HandlerMapping& allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext,
HandlerMapping.class, true, false);
for (HandlerMapping handlerMapping : allRequestMappings.values())
//本项目只需要RequestMappingHandlerMapping中的URL映射
if (handlerMapping instanceof RequestMappingHandlerMapping)
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerM
Map&RequestMappingInfo, HandlerMethod& handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry&RequestMappingInfo, HandlerMethod& requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();
RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();
String requestType = SetUtils.first(methodCondition.getMethods()).name();
PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
String requestUrl = SetUtils.first(patternsCondition.getPatterns());
String controllerName = mappingInfoValue.getBeanType().toString();
String requestMethodName = mappingInfoValue.getMethod().getName();
Class&?&[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName, requestMethodName,
methodParamTypes);
requestToMethodItemList.add(item);
return new ModelAndView(&index&).addObject(&MethodList&, requestToMethodItemList);
这一步我需要说明一下,我这里只用了“RequestMappingHandlerMapping”中的映射信息,其实还有另外三个HandlerMapping,如果想要获取项目中所有的映射信息,肯定是一个都不能放过了。调试的 信息如下:
&!DOCTYPE html&
&html xmlns:th=&http://www.thymeleaf.org&&
&title&Spring Thyme Seed Starter Manager&/title&
&meta http-equiv=&Content-Type& content=&text/ charset=UTF-8&/&
&link rel=&stylesheet& type=&text/css& media=&all& href=&../../css/stsm.css& th:href=&@{/css/stsm.css}&/&
&title&请求方法列表&/title&
&div style=&margin: 0;padding: 0;text-align: center&&&h1&请求方法列表&/h1&&/div&
&li th:each=&method:${MethodList}&&
&h3 th:text=&${method.methodName}&&&/h3&
&p th:text=&'所属控制器:'+${method.controllerName}&&&/p&
&p th:text=&'请求URL:'+${method.requestUrl}&&&/p&
&p th:text=&'请求方式:'+${method.requestType}&&&/p&
&p&方法参数列表:&/p&
&li th:each=&param:${method.methodParmaTypes}&&
&p th:text=&${param}&&&/p&
404是你的controller都没创建对呢!检查下
controller是否已经映射了 打开LOG日志
然后指定到SPRING
看看MVC 打印日志是否映射了/park之类的路径1,排除方法用浏览器访问,是否可以访问2,不能访问就是真的没有映射起来
在非MVC项目中是不可以通过在URL中采用Controller/Action/Param的方式访问的,原因是MVC项目模板的特性(其实是ASP.net MVC开发小组封装好的访问解析方式或映射方式)决定了只有采用了MVC模板的项目才可以基于Controller/Action方式的访问。解决方法:将你在类库项目中的Controller类分离出来,放到MVC项目的Contorller目录中,当然对于与原始类库的类依赖关系,可以通过在该类中引入原始类库的命名空间来解决。这个改动还是需要一点一点调试的,不要怕麻烦,功夫活儿~。
暂无相关文章
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&
Java编程最近更新<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 springmvc webrequest 的文章

 

随机推荐