# Maven / Spring + JBoss - HelloWorld



## filth (17. Mai 2012)

Hallo,

gibt es irgendwo ein HelloWorld Tutorial, welches sich mit dem Spring-Framework, Maven und JBoss beschäftigt? Ich habe Probleme das Ganze unter Eclipse zum Laufen zu bekommen.

Danke!


----------



## Marcinek (17. Mai 2012)

Klar gibt es das.

Mit Google: Tutorial + das was du machen willst

Beispiel:

Turoial Abgasuntersuchung

Tutorial Outlook Installieren.


...

Probiere es aus.


----------



## filth (17. Mai 2012)

Ok dann konkret:


```
14:24:25,126 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/MyHelloWorld].[jsp]] Servlet.service() for servlet jsp threw exception: java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
	at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(DefaultPersistenceUnitManager.java:373) [:3.0.5.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:247) [:3.0.5.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:196) [:3.0.5.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) [:3.0.5.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563) [:3.0.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) [:3.0.5.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) [:3.0.5.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) [:3.0.5.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458) [:3.0.5.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339) [:3.0.5.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306) [:3.0.5.RELEASE]
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127) [:3.0.5.RELEASE]
	at javax.servlet.GenericServlet.init(GenericServlet.java:242) [:1.0.0.Final]
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1208) [:6.0.0.Final]
	at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:955) [:6.0.0.Final]
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:703) [:6.0.0.Final]
	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:541) [:6.0.0.Final]
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:479) [:6.0.0.Final]
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:407) [:6.0.0.Final]
	at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:706) [:6.0.0.Final]
	at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:677) [:6.0.0.Final]
	at org.apache.jsp.index_jsp._jspService(index_jsp.java:60)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [:6.0.0.Final]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) [:6.0.0.Final]
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326) [:6.0.0.Final]
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253) [:6.0.0.Final]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
	at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
	at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
	at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
	at java.lang.Thread.run(Unknown Source) [:1.6.0_27]
```


web.xml:

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</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>
```

spring-servlet.xml:

```
<?xml version="1.0" encoding="windows-1252"?>
<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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
  <!-- Use @Component annotations for bean definitions -->
  <context:component-scan base-package="test"/>
 
  <!-- Use @Controller annotations for MVC controller definitions -->
  <mvc:annotation-driven />
 
  <!-- Add JPA support -->
  <bean id="emf" class=
       "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="loadTimeWeaver">
        <bean class=
 "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
      </property>
  </bean>
 
  <!-- Add Transaction support -->
  <bean id="myTxManager"
     class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
  </bean>
 
  <!-- Use @Transaction annotations for managing transactions -->
  <tx:annotation-driven transaction-manager="myTxManager" />
 
  <!-- View resolver -->
 <bean class=
     "org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/"/>
 </bean>
 
</beans>
```

Controller:

```
package test;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class myTest {

	@RequestMapping(value="/guest")
    public ModelAndView guestbook(HttpServletRequest request) {
		System.out.println("Hello Spring 3.0");
    }
	
}
```

Warum kommt die Exception?


----------



## maki (17. Mai 2012)

> IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}


Das deutet stark auf einen Fehler in deiner persistence.xml hin.

Wenn du keine persistence.xml hast weil du noch gar kein JPA nutzt, solltest du die Config aus deiner spring-servlet.xml entfernen/auskomnentieren.


----------



## filth (17. Mai 2012)

Ok - habe die Zeilen entfernt. Grundsätzlich kommt jetzt unter 

http://localhost:8080/MyHelloWorld/

Die Ausgabe aus der index.jsp


```
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
```

Mein Controller, der in /src/main/java/test liegt:


```
package test;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class myTest implements Controller {
	 
	@RequestMapping(value="/hello")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
 
		String aMessage = "Hello World MVC!";
 
		ModelAndView modelAndView = new ModelAndView("hello_world");
		modelAndView.addObject("message", aMessage);
 
		return modelAndView;
	}
}
```

sollte doch nun unter http://localhost:8080/MyHelloWorld/test erreichbar sein, oder? Dort bekomme ich aber eine 404 Seite


----------



## mvitz (18. Mai 2012)

Da du (laut der obigen web.xml) Spring auf das Pattern *.html gebunden hast, werden grundsätzlich nur Aufrufe, die auf .html enden an Spring geroutet. Versuche also mal: http://localhost:8080/MyHelloWorld/test.html

Ansonsten ist es eher unüblich (afaik), dass der Controller kein URL Pattern bekommt, aber eine Methode. (Beides ist dann wieder normal.)


----------



## filth (18. Mai 2012)

Ok,

habe den Controller so angepasst:


```
package test;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping(value="/test")
public class myTest implements Controller {
	 
	@RequestMapping(value="/hello")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
 
		String aMessage = "Hello World MVC!";
 
		ModelAndView modelAndView = new ModelAndView("hello_world");
		modelAndView.addObject("message", aMessage);
 
		return modelAndView;
	}
}
```

Trotzdem:


```
17:20:01,315 WARN  [org.springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/MyHelloWorld/test/hello.html] in DispatcherServlet with name 'spring'
```

Was mache ich falsch? Eine wirklich schwere Geburt für ein "HalloWorld"


----------



## mvitz (18. Mai 2012)

Hier stand Mist...

Wieso implementierst du eigentlich noch das Controller Interface? Da du ja Annotationen nutzt, sollte das eigentlich nicht nötig sein.


----------



## filth (18. Mai 2012)

Hmm also das RequestMapping über der Methode ist weg. Also:

@Controller
@RequestMapping(value="/test")
public class myTest implements Controller { ... }

Trotzdem kommt unter

http://localhost:8080/MyHelloWorld/test/hello.html
http://localhost:8080/MyHelloWorld/hello.html

jeweils eine 404 Seite


Oh du warst schneller.
Den Controller habe ich irgendwo auf der Spring-HowTo rauskopiert.


----------



## mvitz (18. Mai 2012)

Habe gerade (mit Spring 3.0.7.RELEASE) folgendes getestet (und es funktioniert):


```
package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
@RequestMapping("/test")
public class myTest {

    @RequestMapping(value = "/hello")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String aMessage = "Hello World MVC!";

        ModelAndView modelAndView = new ModelAndView("hello_world");
        modelAndView.addObject("message", aMessage);

        return modelAndView;
    }

}
```

[XML]<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>[/XML]
[XML]<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
Index of /schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Index of /schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Index of /schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  <context:component-scan base-package="test"/>

  <mvc:annotation-driven />

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
  </bean>

</beans>[/XML]

```
<html>
  <head>
    <title>myTest</title>
  </head>
  <body>
    <h1>myTest</h1>
    <p>${message}</p>
  </body>
</html>
```

Folgende Struktur:

```
test
`- WEB-INF
    |- classes
    |   `- test
    |       `- myTest.class
    |- hello_world.jsp
    |- spring-servlet.xml
    `- web.xml
```

Einfacher (und schöner) wäre allerdings folgendes:

```
package test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
@RequestMapping("/test")
public class MyTest {

    @RequestMapping(value = "/hello")
    public ModelAndView hello() {
        return new ModelAndView("hello_world", "message", "Hello World MVC!");
    }

}
```

Ansonsten gibt es zur Bestimmung der View und zum setzen der Variablen für die View noch weitere Wege.


----------



## filth (18. Mai 2012)

Hmm scheinbar mache ich grundsätzlich etwas falsch beim Deployment - es klappt immer noch nicht.

Wie genau muss ich vorgehen, um eine aktuelle Version der Anwendung auf dem JBoss auszuführen? In Eclipse mache ich momentan einen Rechtklick auf das Projekt und "Run on Server"


----------



## mvitz (18. Mai 2012)

Ich habs hier ehrlich gesagt nur auf einem Tomcat versucht, evtl. solltest du das Projekt einmal cleanen (Build --> Clean).


----------



## filth (19. Mai 2012)

Ok ich habe mal ganz von vorne angefangen.

Dieses Tut:
Chapter 1. Basic Application and Environment Setup

Ordnerstruktur:




Controller:

```
package springapp.web;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");

        return new ModelAndView("hello.jsp");
    }

}
```

Aufruf:
http://localhost:8080/springapp4/hello.htm

Fehler:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [web.HelloController] for bean with name '/hello.htm' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]; nested exception is java.lang.ClassNotFoundException: web.HelloController


Liegt mein Controller im falschen Verzeichnis?


----------



## maki (19. Mai 2012)

Das package ist falsch, da fehlt springapp4


----------



## filth (19. Mai 2012)

Stimmt - korrigiert, trotzdem der gleiche Fehler


----------

