2015-03-13 367 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