HTTP状态404 - Spring MVC

时间:2015-11-26 19:13:27

标签: spring spring-mvc

我刚刚在spring工具套装中创建了一个示例spring mvc项目(没有任何修改),当我执行它时,我按照index.jsp获取输出。但之后我只在视图文件夹中添加了一个index.html页面并进行了两次更改

  1. 在控制器类中,我添加了:return' index';

  2.  <beans:property name="prefix" value="/WEB-INF/views/" />
     <beans:property name="suffix" value=".html" />
    
  3. 在Pivotal Server Developer Edition v3.1上更改后执行代码时,我收到HTTP状态404(请求的资源不可用错误。)

    据我所知,不需要进行其他更改。 请提供一些建议。 提前致谢

    HomeController.java

    @Controller
    
    public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
    
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    
        String formattedDate = dateFormat.format(date);
    
        model.addAttribute("serverTime", formattedDate );
    
        return "index";
    }
    
    }
    

    servlet的context.xml中

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    
    <!-- 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=".html" />
    </beans:bean>
    
    <context:component-scan base-package="com.srio.tata" /> 
    

    的web.xml

                 contextConfigLocation的         /WEB-INF/spring/root-context.xml     

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    roo-context.xml

3 个答案:

答案 0 :(得分:1)

您是否配置了viewResolver

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

答案 1 :(得分:0)

由于.html文件是静态并且不需要servlet处理,因此使用<mvc:resources/> mapping会更有效,也更简单。这需要Spring 3.0.4 +。

例如:

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

通过所有以/static/开头的请求传递到webapp/static/目录。

因此,将index.html放入webapp/static/并使用方法中的return "static/index.html";,Spring应该会找到该视图。

请参阅此问题 - https://stackoverflow.com/a/15480301/4531387

答案 2 :(得分:0)

在web.xml中更改来自

的网址格式
<url-pattern>/</url-pattern>

<url-pattern>/*.html</url-pattern>