当控制器重定向时,我无法通过spring mvc获取json数据

时间:2014-05-07 03:37:41

标签: java json spring-mvc

我想从spring mvc返回json和html spring config :(当控制器没有重定向时,没关系!)

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="text/html" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="favorParameter" value="false" />
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
            <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp">
                </property>
            </bean>
        </list>
    </property>
            <!--  ............... -->

然后是控制器:

@RequestMapping(value="test")
public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
    Result result = new Result(/*.......*/);
             //return 
             {
             //====>it is OK,when i call http://localhsot/test.json 
                ModelAndView mv = new ModelAndView("user/test","result",result);                       
                return mv; //
            }
            {
            //=====> it is also redirect,when i call  http://localhsot/test.json (response statues 302!)
                ModelAndView mv = new ModelAndView("redirect:user/test""result",result);
                return mv;
            }
}

如果我的控制器中的ModelAndView&#39;视图是redriect,我如何获取json数据?

0 个答案:

没有答案