显示 404 错误的索引文件
Index file showing 404 error
这是 springs with hibernate 应用程序和使用最新的 eclipse juno。
当我 运行 这个 URL 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>
项目结构:
将您的 index.jsp
直接放在 WebContent
文件夹下。
然后更改您在 web.xml
中的 welcome-file
位置
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
实际上你完全可以跳过 web.xml
中的这个配置。默认情况下,您的应用程序将查找 index.jsp
。
- 现在 URL http://localhost:8080/EasyCCE/ 将 return
index.jsp
。不需要单独的控制器配置。
确保 EasyCCE
是您的 ApplicationName/ContextPath 并且您不会收到任何部署错误。
得到解决方案,
<welcome-file-list>
<welcome-file>/WEB-INF/resources/jsp/index.jsp</welcome-file>
</welcome-file-list>
这是 springs with hibernate 应用程序和使用最新的 eclipse juno。
当我 运行 这个 URL 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>
项目结构:
将您的
index.jsp
直接放在WebContent
文件夹下。然后更改您在
web.xml
中的welcome-file
位置<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
实际上你完全可以跳过 web.xml
中的这个配置。默认情况下,您的应用程序将查找 index.jsp
。
- 现在 URL http://localhost:8080/EasyCCE/ 将 return
index.jsp
。不需要单独的控制器配置。
确保 EasyCCE
是您的 ApplicationName/ContextPath 并且您不会收到任何部署错误。
得到解决方案,
<welcome-file-list>
<welcome-file>/WEB-INF/resources/jsp/index.jsp</welcome-file>
</welcome-file-list>