使用Restful风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制,但今天却只要一用这个,springmvc拼接出来的路径就会有问题!
contrller层的代码:
@Controller
public class HelloController{
@GetMapping("/res/{a}/{b}")
public String restful(@PathVariable int a,@PathVariable int b, Model model) {
int res=a+b;
model.addAttribute("msg",res);
return "hello";
}
}
目录结构:
这里为什么把我的请求路径加到上去了呢?
检查springmvc-servlet.xml配置:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.konan.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
最终发现就是这里出了问题!!!
就在视图拼接这里,WEB-INF前面要写‘/’的,不然的话就会拼接路径就会紊乱,具体原因我也不知道,待日后研究吧
改成:<property name="prefix" value="/WEB-INF/jsp/"/>
成功后访问: