嘿,我对Spring和Java Web开发都很陌生。从昨天起我就一直在想这个问题。我已经编写了一个控制器来处理我对服务器的请求。但每当我试图访问该页面时,我都会遇到tomcat 404错误。
当我检查tomcat cmd提示时,我得到错误
警告:在名为"spring"的DispatcherServlet中找不到URI为[/leaveeap/]的HTTP请求的映射
下面是我用过的文件。
Spring servlet.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="com.imaginea.leaveapp.model" />
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/organization" p:username="root"
p:password="root" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Leave Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
控制器类
package com.imaginea.leaveapp.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.imaginea.leaveapp.model.LeaveApplication;
import com.imaginea.leaveapp.services.LeaveApplicationService;
@Controller
public class LeaveApplicationController {
@Autowired
private LeaveApplicationService leaveApplicationService;
@RequestMapping("/index")
public String leaveList(Map<String, Object> map) {
map.put("leave", new LeaveApplication());
map.put("leaveList", leaveApplicationService.getLeaveDetails());
return "leaveapp";
}
@RequestMapping(value = "/apply", method = RequestMethod.POST)
public String applyLeave(@ModelAttribute("leave") LeaveApplication leave, BindingResult result){
leaveApplicationService.addLeave(leave);
return "redirect:/index";
}
@RequestMapping("/approve/{leaveID}")
public String approve(@PathVariable("leaveID") Integer leaveID){
LeaveApplication leave = leaveApplicationService.getLeaveDetail(leaveID);
leave.setStatus("approved");
leaveApplicationService.updateLeave(leave);
return "redirect:/index";
}
@RequestMapping("/reject/{leaveID}")
public String reject(@PathVariable("leaveID") Integer leaveID, BindingResult result){
LeaveApplication leave = leaveApplicationService.getLeaveDetail(leaveID);
leave.setStatus("rejected");
leaveApplicationService.updateLeave(leave);
return "redirect:/index";
}
}
<context:component-scan base-package="com.imaginea.leaveapp.model" />
您的控制器位于程序包com.imaginea.leaveapp.controller
中。因此,它不会被扫描并用于您的请求。
您似乎缺少url映射文件或leaveap的映射。类似于:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/leaveapp=leaveappController
</value>
</property>
</bean>