Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2007
    Messaggi
    331

    [Java] primi passi con Spring

    Ciao a tutti, sto facendo i primi passi con il framework Spring ma sto avendo qualche problema a far funzionare il mio primo Hello World.
    Dunque seguendo un tutorial trovato online ho creato sotto WEB-INF il file project-servlet.xml nel seguente modo:

    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans 
        xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.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
    	    http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd"
        >
     
        <annotation-driven />
        <context:component-scan base-package="it.telecomitalia.controller" />
    	 
    	<beans:bean id="viewResolver"
    		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<beans:property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<beans:property name="prefix" value="/WEB-INF/jsp/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    </beans:beans>
    e ho scritto il web.xml nel seguente modo:

    codice:
    <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>Archetype Created Web Application</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
        <servlet>
            <servlet-name>project</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>project</servlet-name>
            <url-pattern>/welcome.jsp</url-pattern>
            <url-pattern>/welcome.html</url-pattern>
        </servlet-mapping>
    </web-app>
    La classe controller è così definita:

    codice:
    package it.telecomitalia.controller;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    
    @Controller
    @RequestMapping("/welcome")
    public class HelloWorldController {
    
    
    	@RequestMapping(method = RequestMethod.GET)
    	public ModelAndView helloWorld() {
     
    		String message = "<br><div style='text-align:center;'>"
    				+ "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from CrunchifyHelloWorld.java **********</div><br><br>";
    		return new ModelAndView("welcome", "message", message);
    	}
    	
    }
    e le jsp sono le seguenti:

    index.jsp
    codice HTML:
    <html><head><title>Spring MVC Test web app</title><style type="text/css"></style></head><body>	<br>	<div style="text-align:center">		<h2>			Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>		</h2>		<h3>			<a href="welcome.html">Click here to See Welcome Message... </a>(to			check Spring MVC Controller... @RequestMapping("/welcome"))		</h3>	</div></body></html>
    e welcome.jsp nella cartella WEB-INF/jsp

    codice HTML:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Hello World Spring MVC Example</title><style type="text/css"></style></head><body>    ${message}</body></html>
    
    ma se provo ad accedere a welcome.jsp mi da errore 404 resource not available
    Che sto sbagliando?
    
    Grazie a chi mi aiuterà

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Non ho verificato bene tutto quello che hai scritto ma una cosa mi salta all'occhio: il mapping del DispatcherServlet.
    Generalmente/tipicamente (e se non ci sono esigenze particolari o più "strane"), il DispatcherServlet lo si mappa su:

    <url-pattern>/</url-pattern>

    Dove "/" indica sostanzialmente il "default" servlet, se non ci sono altri mapping più specifici, tutto quanto va al DispatcherServlet ed è poi compito di Spring matchare sui controller.

    Quindi puoi mettere:

    @RequestMapping("/welcome.html")

    E a quel punto:

    http://host:porta/nomecontesto/welcome.html corrisponde.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2007
    Messaggi
    331
    Provo a fare come hai suggerito!

    Niente da fare provando a mettere
    <servlet-mapping>
    <servlet-name>project</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    in web.xml non cambia nulla
    Ultima modifica di DarthSandr; 22-06-2015 a 15:28

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 © 2025 vBulletin Solutions, Inc. All rights reserved.