网址模式“ / *”在MVC项目中不起作用

时间:2019-07-02 10:48:32

标签: spring spring-mvc web model-view-controller web.xml

在spring mvc中的web.xml中使用“ / *”作为url模式时,我没有重定向到页面。 每当我尝试使用以下url时,我都会在控制器方法中获取记录器,但无法获取该页面。

  

尝试了网址:http://localhost:8080/mvc-example

     

记录器:请求主页

     

2019年7月2日下午3:57:35 org.springframework.web.servlet.PageNotFound   noHandlerFound警告:找不到带有URI的HTTP请求的映射   DispatcherServlet中的[/mvc-example/WEB-INF/views/helloAgain.jsp]   名称为“ mvc-example”

  

尝试了网址:http://localhost:8080/mvc-example/hello

     

记录器:已请求首页问候页面

     

2019年7月2日下午3:58:38 org.springframework.web.servlet.PageNotFound   noHandlerFound警告:找不到带有URI的HTTP请求的映射   DispatcherServlet中的[/mvc-example/WEB-INF/views/hello.jsp]名称   'mvc-example'

这是我的项目结构:

我的控制器:

@Controller
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(Locale locale, Model model) {
        System.out.println("Home hello Page Requested");
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printAgainHello(Locale locale, Model model) {
        System.out.println("Home Page Requested");
        model.addAttribute("message", "Hello Again Spring MVC Framework!");
        return "helloAgain";
    }
}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

   <display-name>mvc-example</display-name>

   <servlet>
      <servlet-name>mvc-example</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-example-servlet.xml</param-value>
            </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>mvc-example</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>

</web-app>

mvc-example-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <context:component-scan base-package="com.khan" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

</beans:beans>

当我仅使用“ /”时,它可以按预期工作,但“ / *”无法正常工作。我猜是在犯一些错误。

有人请给我解释一下吗?

谢谢。

0 个答案:

没有答案