SpringMVC 多视图配置 jsonp

最近要在spring mvc 上实现 jsonp 所以就查找 spring 官方文档 发现 org.springframework.web.servlet.view.ContentNegotiatingViewResolver 这个东西可以实现

但是在我写demo 的时候去出现了问题 google了 半天也是没有什么结果 。。。
可是无意间改动一个方法的返回值 却成功了 。。。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.wzl.openapi"></context:component-scan>

<context:annotation-config />

<mvc:annotation-driven />

<!-- 定义JSP视图解析器-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView
</value>
</property>
</bean>

<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="format" />
<property name="ignoreAcceptHeader" value="true" />
<!-- true,开启扩展名支持,false关闭支持 -->
<property name="favorPathExtension" value="true" />
<property name="defaultContentType" value="application/json"></property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="jsonp" value="application/javascript" />
<entry key="jsp" value="text/html"></entry>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"></property>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="com.wzl.openapi.util.MappingJacksonJsonpView"></bean>

</list>
</property>
</bean>

</beans>

没修改之前
	/**
* 获取验证码
* @param mobile
* @return
*/
@RequestMapping( value = "getCode", produces = "application/json;charset=UTF-8" )
public @ResponseBody Map<String, Object> getCode( Model model, String userMobile ) {
Map<String, Object> map = new HashMap<String, Object>();
try{
Map<String, String> _m = bpwm.getCode( userMobile );
map.put("request", "200");
map.put("data", _m);
}catch (Exception e) {
// TODO: handle exception
map.put("request", e.getMessage());
map.put("data", propes.getPropes().get( "getCode.error." + e.getMessage() ));
}
return map;
}

修改之后
	/**
* 获取验证码
* @param mobile
* @return
*/
@RequestMapping( value = "getCode" )
public String getCode( Model model, String userMobile ) {
try{
Map<String, String> _m = bpwm.getCode( userMobile );
model.addAttribute("request", "200");
model.addAttribute("data", _m);
}catch (Exception e) {
// TODO: handle exception
model.addAttribute("request", e.getMessage());
model.addAttribute("data", propes.getPropes().get( "getCode.error." + e.getMessage() ));
}
return "servicesAPI";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值