Jump to content

Spring 3.5 - Primeira aplicação - mapeamento


Recommended Posts

Posted (edited)

Olá a todos,

necessito de começar a utilizar a framework Spring, mas quero utilizar a mesma com as anotações, no entanto os tutoriais que encontro, mesmo os mais básicos, não estão a funcionar...

À primeira vista o context:component-scan não está a detectar os mapeamentos a nível do controler mas parece que está tudo correcto...

Controller


package com.home;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

       @RequestMapping("/hello")
               public ModelAndView helloWorld() {
                       String message = "Hello World, Spring 3.0!";
                       return new ModelAndView("hello", "message", message);
               }
}

View 1

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
       <head>
               <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
               <title>JSP Page</title>
       </head>
       <body>
               <h1>Hello World!</h1>
               <a href="hello.html">Say Hello</a>
       </body>
</html>

View 2

<html>
       <head>
               <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
       </head>
       <body>
               ${message}
       </body>
</html>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
       <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>1</load-on-startup>
       </servlet>
       <servlet-mapping>
               <servlet-name>dispatcher</servlet-name>
               <url-pattern>*.html</url-pattern>
       </servlet-mapping>
       <session-config>
               <session-timeout>30</session-timeout>
       </session-config>
       <welcome-file-list>
               <welcome-file>index.jsp</welcome-file>
       </welcome-file-list>
</web-app>

Dispatcher-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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

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

       <context:component-scan base-package="com.home"/>

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

</beans>

A primeira página é apresentada correctamente mas ao clicar no link sou enviado para:

http://localhost:8080/SpringProj/hello.html

com um erro/warning no servidor:

WARNING: No mapping found for HTTP request with URI [/springProj/hello.html] in DispatcherServlet with name 'dispatcher'

Obrigado

José

Edited by Baderous
geshi
  • 2 weeks later...
Posted

Problem solved...

Dispatcher-Servlet.Xml tinha uma incoerência na configuração.

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

indicava que mapeamento do url deveria obedecer à nomenclatura das classes, enquanto

<context:component-scan base-package="com.home"/>

indicava que mapeamento do url deveria ser feito com base no RequestMapping das classes/métodos.

Removendo primeira linha mecionada, mapeamento funcionou tal como esperado.

  • Vote 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.