索引文件显示404错误

时间:2016-01-25 11:54:23

标签: hibernate jsp spring-mvc http-status-code-404 web.xml

这是使用hibernate应用程序并使用最新的eclipse juno。

当我运行此网址http://localhost:8080/EasyCCE/时,会显示404找不到网页错误

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>EasyCCE</display-name>
  <welcome-file-list>
    <welcome-file>resources/jsp/index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

调度-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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="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 
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd">

         <mvc:annotation-driven />
         <mvc:default-servlet-handler/>
         <mvc:resources mapping="/resources/**" location="/resources/" />
        <context:component-scan base-package="com.slv.controller" /> 

    <!-- JDBC Properties -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/easycce" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- Hibernate Session properties -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="slv.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <!--    ViewResolver Spring Will redirecting -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/resources/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

LoginController.java

package com.slv.controller;

import javax.servlet.http.HttpSession;

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

@Controller
public class LoginController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session) {
        System.out.println("inside");
        return new ModelAndView("index");
    }
}

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    Welcome
</body>
</html>

项目结构:

Project Structure

2 个答案:

答案 0 :(得分:1)

  1. numbers + gcsenum直接放在index.jsp文件夹下。

  2. 然后更改WebContent

    中的welcome-file位置
    web.xml
  3. 实际上,您可以在<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 中完全跳过此配置。默认情况下,您的应用会查找web.xml

    1. 现在,网址http://localhost:8080/EasyCCE/将返回index.jsp。不需要单独的控制器配置。
    2. 确保index.jsp是您的ApplicationName / ContextPath,并且您不会遇到任何部署错误。

答案 1 :(得分:0)

得到解决方案,

  <welcome-file-list>
    <welcome-file>/WEB-INF/resources/jsp/index.jsp</welcome-file>
  </welcome-file-list>