Ale*_*nar 6 java rest spring-mvc
我的spring mvc应用程序有一个ContentNegotiatingViewResolver,它定义了JsonView以呈现json共鸣:
<mvc:annotation-driven/>
<context:component-scan base-package="world.domination.test"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="com.secondmarket.connector.springmvc.MappingJacksonJsonViewEx"/>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
整个应用程序位于根URL"myapp".一切都按照我的需要运作.
第一个问题是:如何在访问某个URL时返回静态html页面?比如,当访问Spring uri/myapp/test时,我想呈现一个位于root webapp文件夹中的html页面/TestStuff.html.
我继续写了一个简单的控制器:
@Controller
@RequestMapping("test")
public class TestConnector {
@Autowired
private RestTemplate tpl;
@RequestMapping(method = RequestMethod.GET)
public String get() {
return "/TestStuff.html";
}
@RequestMapping(method = RequestMethod.POST)
public String post(@RequestParam("url") String url, @RequestParam("data") String data) {
return tpl.postForObject(url, data, String.class, new HashMap<String, Object>());
}
}
Run Code Online (Sandbox Code Playgroud)
get()方法应该告诉Spring呈现TestStuff.html,但是我得到一个错误,说缺少名为"/TestStuff.html"的视图.
在第二个问题是如何避免的必要性,把扩展到的URL.在我的示例中,当我使用/ myapp/test而不是/myapp/test.html时,我的ContentNegotiatingViewResolver使用呈现{}(空花括号)的json视图
任何指针都非常感谢.
不要从控制器返回"/TestStuff.html",而是尝试返回"redirect:/TestStuff.html".
另一种选择是为静态页面创建和注册视图解析器.也许是这样的:
<bean id="staticViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="prefix" value="/WEB-INF/static/"/>
<property name="suffix" value=".html"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19848 次 |
最近记录: |