Visualizzazione dei risultati da 1 a 3 su 3

Hybrid View

  1. #1

    [Spring] Problema con controller annotation

    ciao!

    sto cercando di usare i controller con le annotations.
    questo il controller:
    codice:
    @Controller
    @RequestMapping("/")
    public class IndexController {
    
        @RequestMapping(method = RequestMethod.GET)
        public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView model = new ModelAndView("index");
            model.addObject("msg", "hello world");
            return model;
        }
    
    }
    questo il dispatcher-servlet.xml
    codice:
    <?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:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
            <property name="prefix">
                <value>/WEB-INF/jsp/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    
    </beans>
    e infine l'applicationContext.xml:
    codice:
    <?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:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        
        <context:component-scan base-package="com.mp" />
    
    </beans>
    nel web.xml ho messo così:
    codice:
    <listener>
            <!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
    ma ottengo sempre un HTTP Status 400.
    da cosa può dipendere??

  2. #2
    ho fatto un pò d'ordine diciamo.
    questo il controller:
    codice:
    @Controller
    public class IndexController {
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String home(Locale locale, Model model) {
            Date date = new Date();
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
            String formattedDate = dateFormat.format(date);
            model.addAttribute("serverTime", formattedDate);
            return "index";
        }
    
    }
    questo il web.xml:
    codice:
    <?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/dispatcher-servlet.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>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    e questo il dispatcher-servlet.xml:
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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">
    
        <context:component-scan base-package="com.mp" />
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".html" />
        </bean>
    
    </beans>
    la pagina index.jsp si trova dentro /WEB-INF/views
    questo l'indirizzo a cui vado: http://localhost:8080/spring/index.html
    ottengo questo errore:
    codice:
    HTTP Status 404 -type Status report
    message 
    description The requested resource is not available.
    
    Apache Tomcat/8.0.9
    la cosa strana è che non indica neanche la pagina che non trova!
    problema di "contesto"??

  3. #3
    ok modificando il controller così funziona:
    codice:
    @Controller
    public class IndexController {
    
        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String index(Locale locale, Model model) {
            Date date = new Date();
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
            String formattedDate = dateFormat.format(date);
            model.addAttribute("serverTime", formattedDate);
            return "index";
        }
    
    }
    mi sorge però una domanda.
    il metodo deve ritornare una stringa che equivale al nome del file senza estensione.
    ma che relazione c'è tra il nome del metodo e il valore di value passato come parametro a RequestMapping??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.