我正在试图弄清楚我将如何构建一个restFull Web服务器:
1:转到/
时提供单个index.html文件2:所有其他URL将以安静的方式被控制器捕获 例如: / invoke1 - >将到达“/ invoke1”的请求映射
问题:
首先,Spring调度程序有一些奇怪的后果与服务html页面(我不是在讨论资源和<mvc:resources mapping
)我说的是服务html页面而不是JSP。
我通过使用默认的catalina servlet解决了这个问题:
<servlet> <servlet-name>default</servlet-name> <servlet-class> org.apache.catalina.servlets.DefaultServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/docs/*</url-pattern> </servlet-mapping>
使用此代码我可以提供HTML页面(即使在安全性之后,只要它们在/ docs / *路径下),所以从这里我可以做到:
@RequestMapping(method = RequestMethod.GET) public String main() { return "redirect:/docs/index.html"; }
但这不是我想要的。从这里开始,浏览器上的URL将显示为/ docs /
我想要发生的是当用户进入/他将获得index.html但是当他进入/ invoke1时他会到达弹簧控制器。
这是我现在想要解决的问题。希望你能指导我找到合适的解决方案。 谢谢。
答案 0 :(得分:0)
1)最简单的解决方案是将Spring映射到类似/rest/
的内容,并为/
留下index.html
。
2)另一个解决方案,尽管你不想要它,将你的index.html
包装在JSP中并在/index.html
上提供,并按照预期定义你的控制器。
3)或使用&lt; mvc:resources&gt;。您可以从预定义的静态目录中提供.html
,只需在索引控制器“static / index.html”中返回。比照这个StackOverflow问题。
答案 1 :(得分:0)
在配置文件中,您可以定义一个mvc:view-controller
来处理重定向网址:
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
.....
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
....>
<mvc:view-controller path="/index" view-name="/docs/index.html" />
....
</beans>
您可以将main
方法更改为重定向到/index
:
@RequestMapping(method = RequestMethod.GET)
public String main() {
return "redirect:/index";
}
现在,当向/
发出请求时,它会转到main
方法,该方法会向/index
发送重定向请求。上述映射会将/index
的请求转发给静态视图index.html
。浏览器网址中不会显示/docs/index.html
。
答案 2 :(得分:0)
使用Spring 3+及其资源处理,您可以这样做:
/
- 所有请求(*.jsp
除外)都将由Spring处理index.html
将由Spring提供服务)index.html
配置为web.xml中的<welcome-file>
答案 3 :(得分:0)
简单的答案是你需要使用forward(“forward:/”)而不是重定向(“redirect:/”)。