Issue
I am new to Spring MVC and I am trying to make simple HelloWorld program i am following this tutorial https://www.javaguides.net/2018/10/spring-mvc-5-hello-world-example.html
AppConfig
package main.java.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"main.java"
})
public class AppConfig {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Dispatcher Servlet Initializer
package main.java.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletDispatcherInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class <?> [] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class <?> [] getServletConfigClasses() {
return new Class[] {
AppConfig.class
};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
Controller
package main.java.controller;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import main.java.model.HelloWorld;
@Controller
public class JavaController {
@RequestMapping("/helloworld")
public String handler(Model model) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Hello World Example Using Spring MVC 5!!!");
helloWorld.setDateTime(LocalDateTime.now().toString());
model.addAttribute("helloWorld", helloWorld);
return "helloworld";
}
}
Model
package main.java.model;
public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
JSP
...
${helloWorld.message}</h2>
Server date time is : ${helloWorld.dateTime}
...
File Path
Error
i tried cleaning the server, close and open the browser, republish my project.
I don't know what to do now, please help me.
my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringMVC5</groupId>
<artifactId>SpringMVC5</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc5-helloworld-exmaple Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
Solution
1-
You should list all your packages including configuration classes in @ComponentScan annotation instead main.java :
@ComponentScan(basePackages = {"config","controller"})
Or
Move all your packages inside a parent package (example : com.project) like com.project.config, com.project.controller, com.project.model and use it:
@ComponentScan(basePackages = {"com.project"})
2- Check the web dependency assembly of your project, it should be like this:
If is your config not like this, you should add it by clicking on Add button and selecting java Build Path Entries.
And select Maven Dependencies in the next window.
Answered By - tcharaf
Answer Checked By - Pedro (JavaFixing Volunteer)