km1*_*km1 18 java tomcat spring-mvc
我有一个Spring Controller,它有几个RequestMappings用于不同的URI.我的servlet是"ui".servlet的基URI仅适用于尾部斜杠.我希望我的用户不必输入尾部斜杠.
此URI有效:
http://localhost/myapp/ui/
Run Code Online (Sandbox Code Playgroud)
这个没有:
http://localhost/myapp/ui
Run Code Online (Sandbox Code Playgroud)
它给了我一个HTTP状态404消息.
我的web.xml中的servlet和映射是:
<servlet>
<servlet-name>ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ui</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我的控制器:
@Controller
public class UiRootController {
@RequestMapping(value={"","/"})
public ModelAndView mainPage() {
DataModel model = initModel();
model.setView("intro");
return new ModelAndView("main", "model", model);
}
@RequestMapping(value={"/other"})
public ModelAndView otherPage() {
DataModel model = initModel();
model.setView("otherPage");
return new ModelAndView("other", "model", model);
}
}
Run Code Online (Sandbox Code Playgroud)
and*_*dyb 13
例如,如果Web应用程序存在于Web服务器的webapps目录中,webapps/myapp/则可以在http://localhost:8080/myapp/假定默认Tomcat端口的情况下访问此应用程序上下文的根目录.这应该使用或不使用尾部斜杠,我认为默认情况下 - Jetty v8.1.5中就是这种情况
一旦你点击/myappSpring DispatcherServlet接管,就将请求路由到<servlet-name>你的配置web.xml,在你的情况下是/ui/*.
DispatcherServlet然后将所有请求路由http://localhost/myapp/ui/到@Controllers.
在控制器本身可以使用@RequestMapping(value = "/*")的炫魅()方法,这将导致两个http://localhost/myapp/ui/和http://localhost/myapp/ui被路由到炫魅() .
注意:由于SPR-7064,您还应该使用Spring> = v3.0.3
为了完整性,以下是我测试过的文件:
package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UiRootController {
@RequestMapping(value = "/*")
public ModelAndView mainPage() {
return new ModelAndView("index");
}
@RequestMapping(value={"/other"})
public ModelAndView otherPage() {
return new ModelAndView("other");
}
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="false">
<servlet>
<servlet-name>ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- spring automatically discovers /WEB-INF/<servlet-name>-servlet.xml -->
</servlet>
<servlet-mapping>
<servlet-name>ui</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:order="2"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
还有2个JSP文件WEB-INF/views/index.jsp和WEB-INF/views/other.jsp.
结果:
http://localhost/myapp/ - >目录列表http://localhost/myapp/ui和http://localhost/myapp/ui/- > index.jsphttp://localhost/myapp/ui/other和http://localhost/myapp/ui/other/- > other.jsp希望这可以帮助!
use*_*316 11
使用Springboot,我的应用程序可以通过将@ RequestMapping的"value"选项设置为空字符串来回复斜杠和不斜杠:
@RestController
@RequestMapping("/some")
public class SomeController {
// value = "/" (default) ,
// would limit valid url to that with trailing slash.
@RequestMapping(value = "", method = RequestMethod.GET)
public Collection<Student> getAllStudents() {
String msg = "getting all Students";
out.println(msg);
return StudentService.getAllStudents();
}
}
Run Code Online (Sandbox Code Playgroud)
PathMatchConfigurer api允许您配置与URL映射和路径匹配相关的各种设置.根据最新版本的spring,默认情况下启用了路径路径匹配.要进行自定义,请查看以下示例.
对于基于Java的配置
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(true);
}
}
Run Code Online (Sandbox Code Playgroud)
对于基于XML的配置
<mvc:annotation-driven>
<mvc:path-matching trailing-slash="true"/>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)
对于@RequestMapping("/ foo"),如果尾部斜杠匹配设置为false,则为example.com/foo/!= example.com/foo,如果设置为true(默认值),则为example.com/foo/ == example .COM /富
干杯!
| 归档时间: |
|
| 查看次数: |
31667 次 |
| 最近记录: |