[英]HTTP Status 404 - tomcat 7
我正在构建一个Spring + Hibernate应用程序。 运行项目时出现404错误。 编译器未显示任何错误消息,它指示应用程序服务器(tomcat 7)已加载servlet,但未显示控制器类中指示的根页。
请帮助......
Config .....`
<?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>
问题在于,您仅将调度程序Servlet映射到* .html,但是在不使用.html后缀的情况下映射了Controllers。
为了使它起作用,可以用不同的方式映射您的控制器,即:
@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/
或http://somehost:someport/YourApplication/index
将导致调用相同的控制器方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.