2015-03-13 130 views
0

我正在构建一个spring + hibernate应用程序。当我运行该项目时,出现404错误。编译器不显示任何错误消息,它表示应用程序服务器(tomcat 7)加载servlet,但不显示控制器类中指示的根页面。HTTP状态404 - tomcat 7

请帮助......

配置..... `

<?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" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

    <context:property-placeholder location="classpath:resources/database.properties" /> 
    <context:component-scan base-package="langS.com" /> 

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <bean id="jspViewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${database.driver}" /> 
     <property name="url" value="${database.url}" /> 
     <property name="username" value="${database.user}" /> 
     <property name="password" value="${database.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.langS.model.Employee</value> 
       <value>com.langS.EmployeeBean</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="hibernateTransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
</beans>` 

控制器

@Controller 
public class NewController { 

    @Autowired 
    private EmployeeService employeeService; 

    @RequestMapping(value = "/index", method = RequestMethod.GET) 
    public String welcomeHome(){ 
     return "index"; 
    } 

    @RequestMapping(value = "/addEmployee", method = RequestMethod.GET) 
    public String addEmployeeRecord(){ 
     return "addEmployee"; 
    } 

    @RequestMapping(value = "/employeeList", method = RequestMethod.GET) 
    public String listEmployeesPage(){ 
     return "employeesList"; 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

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

    <servlet-mapping> 
     <servlet-name>sdnext</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 

</web-app> 
+0

你能访问你的服务器:localhost:8080吗? – Sarz 2015-03-13 12:20:10

+0

是的,我可以。我曾尝试以三种方式访问​​它,部署项目,在eclipse中运行它,以及我的浏览器。 – 2015-03-13 12:36:54

+0

请再次验证您的网址,请注意:您的网页名称区分大小写。 – Sarz 2015-03-13 12:59:29

回答

0

404这意味着,您的tomcat服务器正在获取请求,但现在不会如何处理给定的请求路径。

换句话说,你在你的URL中有一个错误,或者你的配置不符合你的要求。

我们需要您的配置和所需的URL来帮助您。

+0

我已经发布了上面的代码 – 2015-03-13 12:30:56

0

问题是,您只将调度程序servlet映射到* .html,但是您将控制器映射为不使用.html后缀。

要使其工作,要么你映射不同的控制器,即:

@RequestMapping(value = "/index.html", method = RequestMethod.GET) 
public String welcomeHome(){ 
    return "index"; 
} 

或(和我会做什么): 地图你分发程序Servlet在web.xml中的所有请求:

<servlet-mapping> 
    <servlet-name>sdnext</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

然后如果你想用URL看起来像这样打电话给你的应用程序:

http://somehost:someport/YourApplication/ 

然后,你需要映射一些控制器方法是这样的:

@RequestMapping(value = {"/", "/index"}, method = RequestMethod.GET) 
public String home() { 
    // your code here 
} 

因此,呼叫http://somehost:someport/YourApplication/ OR http://somehost:someport/YourApplication/index将导致在同一个控制器方法被调用。

+0

我已经映射了它,我希望它首先加载索引页 – 2015-03-13 12:40:44

+0

我没有看到问题中的映射。你还可以显示你的调度器servlet映射吗? (在web.xml中) 如果你明确地调用http:// somehost:someport/YourApplication/index,你会看到你的索引页吗? – baraber 2015-03-13 12:43:40

+0

当我在浏览器的地址栏中显式调用它时,在服务器启动后,我仍然收到404错误。请在开头看到上面的问题,我发布了2个代码 – 2015-03-13 12:49:36