custom view:
public class MyView extends AbstractView {
.... awesome stuff ...
}
controller:
@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>
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
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;
}
and then this
<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...
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...
Any Ideas?
精彩评论