小编Ale*_*nar的帖子

如何更改Spring MVC处理url'dot'字符的行为

我正在尝试将一个Web项目从Jersey迁移到Spring MVC 3.0.这个过程非常简单,直到我开始使用点符号迁移应该处理URL的控制器:"/ myApp/resources/create/root.subFolder1 ".Spring MVC似乎无耻地从URL中删除了".subFolder1"部分,它发生在框架代码内部(参见AbstractUrlHandlerMapping类)

uriTemplateVariables.putAll(getPathMatcher().extractUriTemplateVariables(matchingPattern, urlPath));
Run Code Online (Sandbox Code Playgroud)

所以我的控制器方法是用root path参数调用的,而不是root.subFolder1

我真的想找到一种方法来自定义这种行为.有什么建议吗?

PS.该要求有点保持现有的URL结构,即切换到查询参数"/ myApp/resources/create/?path = root.subFolder1 "的变通方法,我无法考虑.

PS.我的Spring配置看起来像

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

<context:component-scan base-package="my.app.pkg"/>
Run Code Online (Sandbox Code Playgroud)

java spring-mvc

8
推荐指数
2
解决办法
1万
查看次数

从Controller转发到静态html页面

我的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视图 …

java rest spring-mvc

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×2

spring-mvc ×2

rest ×1