2010-11-29 12 views
2

info d'erreur: Aucun mappage trouvé pour la requête HTTP avec l'URI [/TestSpringWebApp/hello.htm]ressort 3 mvc Aucune correspondance trouvée pour la requête HTTP avec l'URI

Toute aide serait grandement appréciée! J'utilise l'annotation pour mapper la demande au contrôleur. code du contrôleur:

@Controller 
@RequestMapping("/hello.htm") 
public class HelloController {  
    protected final Log logger = LogFactory.getLog(getClass()); 

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)  
      throws ServletException, IOException { 
     String now = (new Date()).toString(); 

     logger.info("Returning hello view with " + now); 

     return new ModelAndView("hello", "now", now);  
    }  
} 

Dispatcher-servlet.xml comme suit:

<?xml version="1.0" encoding="UTF-8"?> 
<beans ...> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <!-- 
    Most controllers will use the ControllerClassNameHandlerMapping above, but 
    for the index controller we are using ParameterizableViewController, so we must 
    define an explicit mapping for it. 
    --> 
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="index.htm">indexController</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 

    <!-- 
    The index controller. 
    --> 
    <bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
      p:viewName="index" /> 
    <!-- <bean name="/hello.htm" class="com.kibboko.poprocks.web.HelloController"/>--> 
</beans> 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans ...> 

    <!--bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties" /> 

    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.url}" 
      p:username="${jdbc.username}" 
      p:password="${jdbc.password}" /--> 

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 

</beans> 

beans.xml 

<?xml version="1.0" encoding="UTF-8"?> 
<beans ...> 

    <!--bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties" /> 

    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.url}" 
      p:username="${jdbc.username}" 
      p:password="${jdbc.password}" /--> 

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 

</beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app ...> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

Répondre

5

Vous devez ajouter @RequestMapping t o la méthode, pas à la classe. Si vous l'ajoutez à la classe, il sera appliqué en tant que préfixe à tous les mappages de méthodes de cette classe, mais vous devrez également mapper les méthodes de la classe.

+0

ajouté @RequestMapping (méthode = RequestMethod.GET) méthode ci-dessus. ne fonctionne toujours pas. Ai-je besoin de configurer quelque chose comme le paquet d'analyse automatique xyz afin que Spring puisse trouver des contrôleurs? Merci un million. – Bobo

+0

@Bobo Avez-vous comme dit nickdos? – Javi

+1

Salut Javi, je n'avais pas en premier lieu. Mais après l'avoir ajouté, cela fonctionne finalement. Merci beaucoup pour votre aide et aussi nickdos. – Bobo

11

Vous devez également ajouter

<mvc:annotation-driven />

à votre fichier Dispatcher-servlet.xml.

+0

voir: http://stackoverflow.com/a/3978283/2893073 – Eddy

2

Vous pouvez avoir @RequestMapping pour la classe aussi, mais chaque méthode doit aussi avoir sa propre « @RequestMapping » S'il vous plaît voir exemple ci-dessous.

@Controller 
@RequestMapping("/appointments") 
public class AppointmentsController { 

    private final AppointmentBook appointmentBook; 

    @Autowired 
    public AppointmentsController(AppointmentBook appointmentBook) { 
     this.appointmentBook = appointmentBook; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public Map<String, Appointment> get() { 
     return appointmentBook.getAppointmentsForToday(); 
    } 

    @RequestMapping(value="/{day}", method = RequestMethod.GET) 
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) { 
     return appointmentBook.getAppointmentsForDay(day); 
    } 

    @RequestMapping(value="/new", method = RequestMethod.GET) 
    public AppointmentForm getNewForm() { 
     return new AppointmentForm(); 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public String add(@Valid AppointmentForm appointment, BindingResult result) { 
     if (result.hasErrors()) { 
      return "appointments/new"; 
     } 
     appointmentBook.addAppointment(appointment); 
     return "redirect:/appointments"; 
    } 
}