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
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;
}
}
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[] {
"/"
};
}
}
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";
}
}
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;
}
}
...
${helloWorld.message}</h2>
Server date time is : ${helloWorld.dateTime}
...
i tried cleaning the server, close and open the browser, republish my project.
I don't know what to do now, please help me.
<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>
One of the possible issues is that you package naming is wrong. Folder structure for maven projects looks like that:
src
|_ main
| |_ java
| | |_ your.actual.package
| |_ resources
| |_ webapp
|_ test
|_ java
|_ your.actual.package
So your actual package starts right after java
folder. That means that you actually have:
config
package instead of main.java.config
for AppConfig
class
controller
package instead of main.java.controller
for JavaController
model
package instead of main.java.model
for HelloWorld
Also you need to change your @ComponentScan
. Leave it empty to scan root package. Or try to use basePackageClasses
if that doesn't work.
Afterwards you need to check you war
file. Unpack it and check that WEB-INF
folder contains .class
files for all created classes and views
has your helloworld.jsp
file.
I hope this will help.
Not sure where is exactly the problem, but here is a complete project for you. You can get help from here:
https://github.com/imrangthub/BlogSolutionUsingSpringHibernateWithClassLevelConfig
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.