HTTP状态404 - tomcat 7

时间:2015-03-13 12:14:46

标签: java spring hibernate java-ee

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

请帮助......

配置..... `

<?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>

2 个答案:

答案 0 :(得分:0)

404表示您的tomcat服务器正在获取请求,但现在不知道如何处理给定的请求路径。

换句话说,您的网址中存在错误,或者您的配置不符合您的要求。

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

答案 1 :(得分: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>

然后,如果您想使用如下所示的网址调用您的应用程序:

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将导致调用相同的控制器方法。