控制器和视图解析器如何正常工作?

时间:2018-10-31 14:13:21

标签: java spring eclipse spring-mvc

我有这个可以正常工作的春季mvc项目

  

在com.crunchify.controller包中

package com.crunchify.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
 * author: Crunchify.com
 * 
 */

@Controller
public class CrunchifyHelloWorld {

    @RequestMapping("/welcom")
    public ModelAndView helloWorld() {
        System.out.println("this is a test");   

        String message = "<br><div style='text-align:center;'>"
                + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CrunchifyHelloWorld.java **********</div><br><br>";
        return new ModelAndView("welcome", "message", message);
    }
} 
  

这是调度程序servlet文件:

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

    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.crunchify.controller" />
    <mvc:default-servlet-handler />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
  

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>CrunchifySpringMVCTutorial</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

      <servlet>
        <servlet-name>crunchify</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>crunchify</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/index.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>*.html</url-pattern> 
    </servlet-mapping>

,我的网页如下:

WEB-INF / jsp /中的页面welcome.jsp:

<html>
<head>
<title>Spring MVC Tutorial by Crunchify - Hello World Spring MVC
    Example</title>
<style type="text/css">
body {
    background-image: url('https://cdn.crunchify.com/bg.png');
}
</style>
</head>
<body>${message}

    <br>
    <br>
    <div
        style="font-family: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align: center;">

        Spring MCV Tutorial by <a href="https://crunchify.com">Crunchify</a>.
        Click <a href="https://crunchify.com/category/java-tutorials/"
            target="_blank">here</a> for all Java and <a
            href='https://crunchify.com/category/spring-mvc/' target='_blank'>here</a>
        for all Spring MVC, Web Development examples.<br>
    </div>
</body>
</html>
  

index.jsp:

<html>
<head>
<title>Spring MVC Tutorial Series by Crunchify.com</title>
<style type="text/css">
body {
    background-image: url('https://cdn.crunchify.com/bg.png');
}
</style>
</head>
<body>
    <br>
    <div style="text-align: center">
        <h2>
            Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>
        </h2>
        <h3>
            <a href="welcome.html">Click here to See Welcome Message... </a>(to
            check Spring MVC Controller... @RequestMapping("/welcome"))
        </h3>
    </div>
</body>
</html>

我的问题是:当我打开网址https://localhost:8080/project-name/welcom.html时,我会得到一个工作页面。

为什么网址https://localhost:8080/project-name/welcom给出404

0 个答案:

没有答案