I'm very new to the Java landscape (Eclipse, Spring, etc.) and just walking through some introductory tutorials at the moment. I just finished a Hello World tutorial for Spring MVC, with a simple page that then posts to another action. Straightforward enough.
I then went on to tinker a bit with the working result of that tutorial. My first attempt is to create another project in Eclipse with just some simple packages containing POJOs. The idea being to have something portable which different applications could use. (Intended to be the equivalent of a "Class Library" project in the .NET world.)
That project has one package, with one interface (IWorld
) and one class (HelloWorld
). In my Spring MVC project I then add the other project to the build path and create an instance of that object in the controller. The compiler says everything's fine:
package com.sandbox.application.mvctest.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.sandbox.domain.models.*;
@Controller
public class HelloWorldController {
@RequestMapping("/")
public String hello() {
IWorld world = new HelloWorld();
world.speak();
return "hello";
//TODO: Return a ModelAndView instead
}
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi(@RequestParam("name") String name, Model model) {
String message = "Hi " + name + "!";
model.addAttribute("message", message);
return "hi";
}
}
The .speak()
method doesn't do anything of note, just returns a value which the controller isn't using yet. The intent of course is to start building up some features in the models rather than just in the controllers and views, building more of a rich domain model.
Eclipse doesn't complain about the code, everything seems to compile just fine. But when executing on Tomcat I get an exception:
Jul 05, 2017 12:36:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [springDispatcher]
java.lang.ClassNotFoundException: com.sandbox.domain.models.IWorld
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
at java.lang.Class.getDeclaredMethods0(Native Method)
[and so on...]
My Googling has proved fruitless so far, likely because I don't always understand what I'm finding. For example, there are some posts which advise to correct something in a pom.xml
file. I have no such file. Is this related to Spring's dependency injection? Should I be using that? I wouldn't have considered this to be an injectable dependency, I'm just creating an instance directly. But then I'm also approaching this from a deeply dogmatic .NET mindset, so all bets are off.
What am I missing here?