springmvc和url-pattern

时间:2016-04-21 01:41:09

标签: spring spring-mvc url-pattern

的web.xml

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

控制器

@Controller
@RequestMapping("/car/*")
public class CarController extends BaseController {

    @RequestMapping("baojia.html")
    public ModelAndView baojia() {
        ModelAndView view = new ModelAndView();
        view.setViewName("baojia");
        return view;
    }

当我访问http://mydomain/car/baojia.html时出现此错误:

[carloan]2016-04-21 09:01:31,177 WARN [org.springframework.web.servlet.PageNotFound] - <No mapping found for HTTP request with URI [/views/baojia.jsp] in DispatcherServlet with name 'springMVC'>

spring.xml ViewResolver

<bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="cache" value="false"/>
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="prefix" value="/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>

我在/views/boajia.jsp

中有文件

我是否作家,它不起作用

<mvc:resources mapping="/views/" location="/views/**" />

我有另一个问题,我不想匹配这个url-pattern:/ api / * 控制器是:

@Controller
@RequestMapping("/api/*")
public class CarApiController extends BaseController {

    @RequestMapping("get")
    @ResponseBody
    public JsonResult getCars()

但它无法正常工作

2 个答案:

答案 0 :(得分:0)

尝试 @RequestMapping(&#34; / car&#34;)而不是 @RequestMapping(&#34; / car / *&#34;) < / p>

并查看以下两个链接,了解如何定义请求映射。

can anybody explain me difference between class level controller and method level controller..?

http://duckranger.com/2012/04/advanced-requestmapping-tricks-controller-root-and-uri-templates/

答案 1 :(得分:0)

URL映射声明不正确使用@RequestMapping(“/ car”)和@RequestMapping(“/ baojia.html”)