0
votes

I am trying to implement an embedded jetty server using spring mvc for dispatching. I tried several tutorials but monstyl they are out of date or don't use embedded jetty.

So far I created a new spring mvc project from scratch in intellij idea.

  • src/

    • main/
      • java/
        • com.springapp.mvc/
          • HelloController.java
          • Launcher.java
      • webapp/
        • WEB-INF/
          • mvc-dispatcher-servlet.xml
          • web.xml
  • pom.xml

HelloController.java

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
}

Launcher.java

public class Launcher {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        //todo: implementation to use dispatcher-servlet...

        server.start();
        server.join();
    }

}

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

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

</beans>

web.xml

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

How do I tell the server correctly to use the dispatcher servlet? I tried a few things out but nothing really worked yet.

1
See here. You will need to switch up your web.xml possibly.Sotirios Delimanolis
thanks that brought me to the right solution :)kitingChris

1 Answers

0
votes

The solution for everyone who is also interested... (in scala not in java)

val server: Server = new Server(8080)
private val root: WebAppContext = new WebAppContext

root.setContextPath("/")
root.setDescriptor("src/main/webapp/web.xml")
root.setResourceBase("src/main/webapp/"))
root.setParentLoaderPriority(true)
server.setHandler(root)

server.start()
server.join()