有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java HTTP状态404 tomcat 7

我正在构建一个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";
    }
}

网络。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) 个答案

  1. # 1 楼答案

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

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

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

  2. # 2 楼答案

    问题是,您只将dispatcher servlet映射到*。html,但不使用。html后缀

    要使其发挥作用,请以不同的方式映射控制器,即:

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

    或者(我会怎么做): 将您的dispatcher 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/http://somehost:someport/YourApplication/index将导致调用相同的控制器方法