使用自定义视图解析器和自定义视图的Spring MVC AJAX和JSON [英] Spring MVC AJAX and JSON using Custom View Resolver, and Custom View

查看:144
本文介绍了使用自定义视图解析器和自定义视图的Spring MVC AJAX和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义视图:

public class MyView extends AbstractView {
   .... awesome stuff ...
}

控制器:

@RequestMapping(value="mylocation")
public ModelAndView dosomething() {
...
   modelAndView.setView( new MyView() );
   return modelAndView;
}

由于某些原因,这不起作用...唯一的视图解析器我有如下:

For some reason this doesn't work... The only view resolver I have is the following:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" >
        <property name="exposedContextBeanNames">
            <list>
                <value>spEnv</value>
            </list>
        </property>
    </bean>

此代码不起作用,因为它试图基于...创建一些JSP视图我不确定,我的表格提交的路径;显然没有JSP视图,我希望实际返回JSON,但由于某种原因这不起作用,它只是试图转发给我一些JSP,所以我猜我需要指定如何在我的XML中处理这个......但我已经看到人们返回JSON的大约1000种不同的方式,而且所有这些对我来说都很混乱,我只是寻找最简单的方法,所以我可以从那里

this code doesn't work because it's trying to create some JSP view based on... I'm not sure, the path of my form submission; and there obviously is no JSP view for it, I'm expecting JSON to be returned actually, but for some reason this isn't working, it's just trying to forward me to some JSP, so I'm guessing that I need to specify how to handle this in my XML... but I've seen about 1000 different ways that people return JSON, and all of them are very confusing to me, I'm just looking for the simplest way, so I can take it from there

编辑:我添加了一个好的开头答案,但是它允许你在任何URL之后键入.json,如果它会做一些非常糟糕的事情,如果作为控制器,我不期待它,所以我需要以某种方式让这个视图解析器只适用于安全的URL

edit: I added an answer which is a good start, but it allows you to type ".json" after any URL and it will do some really bad things if as the controller I'm not expecting it, so I need to somehow have this view resolver only apply to secured URLs

推荐答案

好的,最后尝试了一百万个组合后

Ok, finally after trying a million combinations

@RequestMapping(value = "/test/data")
    public TestFormData dostuff() {
        TestFormData data = new TestFormData();
        data.setName("myname");
        return data;
    }

然后这个

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            </list>
        </property>
    </bean>

我真的不明白如何从这里返回一个对象......

I don't really understand how I can return an object from here...

但实际上,这虽然简单,但非常有用是不可接受的,因为那时我去了我网站的其他页面,并在网址之后放了一个任意的.json抛出了一个异常,它给用户提供了太多关于我的应用程序的信息,所以基本上我需要想办法在某些受保护的控制器上只有这种能力...

But this actually, while simple, and very useful is not acceptable because then I went to some other page of my website, and just put an arbitrary ".json" after the url and it threw an exception that gives the users WAY too much information about my application, so basically I need to figure out a way to only have this ability on certain controllers that are secured...

任何想法?

这篇关于使用自定义视图解析器和自定义视图的Spring MVC AJAX和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆