M
M
MartyMcAir2020-11-16 22:49:06
Java
MartyMcAir, 2020-11-16 22:49:06

What are the options for running spring-webmvc?

Good day.
I'm learning Spring Framework and tried different variations of its usage.
There are many variations of running Spring-based applications, it's easy to get confused.
I bought the course in the hope that after the course, everything will fall into place and a deeper understanding of Spring will come).

In the course, the project is created in IDEA using the maven archetype: maven-archetype-webapp.
But the problem itself is that the author of the course uses IDEA Ultimate, which of course I don't have (already). (In the course, the author connects TomCat.)
Is it possible to run this project using Spring tools, without using IDEA Ultimate, and if so, how?
What dependencies in pom.xml need to be included, and how to configure, so that everything would work?

Project structure from the course:
5fb2cbb36ccd6901463418.jpeg
Files:
HomeController.java:

@Controller
public class HomeController {
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView home() {
        return new ModelAndView("index");    }   }

app-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<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">
    <context:component-scan base-package="org.example"/>
</beans>

web-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="org.example"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".html"/>
    </bean>
</beans

index.html - nothing interesting, inside body, h2 Hello World tag and all
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app-config.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfiguration</param-name>
            <param-value>classpath:web-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>my-dispatcher-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

In the pom.xml file, the author of the course left everything as it is, i.e. standard generated with maven archetype: maven-archetype-webapp.. And only one additional dependency was added (yes it is spring-webmvc):
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>


For example.. Tried the official guide
https://spring.io/guides/gs/rest-service/ - their sample project runs if:
0) Spring on the page recommends using https://adoptopenjdk.net/ .
1) remove dependency spring-boot-starter-tomcat..
2) IDEA complained about spring-boot-maven-plugin does not found..
Fixed by adding dependency.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.3.4.RELEASE</version>
    <type>maven-plugin</type>
</dependency>

3) Configure to be used everywhere by the compiler and in the Java 1.8 or 11 project.
4) Create a simple @RestController and other project files
4) After the first launch of the application, in the IDEA config, add to the VM options field:
-Ddebug=true And restart in Debug mode.
5) After launch, look in the browser for the result: localhost:8080/greeting
6) Change something in the GreetingController file, go to the Build -> Recompile menu (or chain in the hotkey settings so that IDEA recompiles all changed files.)
and Spring itself picks up the recompiled file. And the changes are visible in the beauty browser).
And IDEA Ultimate was not needed.

I tried everything .., to transfer the project from the course, along with all the xml configs, to spring boot .. But it doesn’t work, and something tells me that this shouldn’t work like that, these two projects are configured completely differently.

(Teachers from the course raise doubts in understanding what they are doing .. They
explain little of the essence, no step by step what for and how, more like a dry instruction without understanding. - hence the difficulty I can’t understand how to start this project without IDEA Ultimate, by means of Spring'a., my knowledge and understanding of Spring'a is not enough.)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MartyMcAir, 2020-11-18
@MartyMcAir

1) Yes, I agree. (meaning that it can connect the built-in serv.)
2) sorry)
.
3) Good, I agree. Now for me, all the use of maven is to connect dependency and only, it would be nice to understand it in more depth ..
4) Spring Boot IDEA is not needed to start - this is understandable.
SpringBoot - based on the Spring Framework, and by connecting one spring-boot-* ..-starter, you can immediately connect a bunch of libraries (including, of course, there will be libraries with the Spring Framework) and + all the SpringBoot goodies, auto configuration and other..
- So .. it means theoretically it is possible, if you analyze Spring''à well enough, and connect to such a project only what is necessary to run it like with SpringBoot .., but without any additional. dependencies that it automatically connects ..
After all, the main feature of SpringFramework is its maximum flexibility, which means that theoretically there should be a way by connecting something else, so that it would work like SpringBoot - but without its additional . dependencies.
Thanks for the page.
I didn't even think about SpringTools4 for IDEs. I saw they write that there is some kind of "helper tool" for Spring, for Ecipse and that's it, but it turns out that it is also for Visual Studio. Should be o.k. useful, ok we'll see, we'll try.
UPD: it was necessary to add a trace. dependencies..

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

And for a successful redirect to the index. page, throw on a separate view mapping controller: @GetMapping(value = "/"..
git sources (various variations.., of course not in...
Meditation helped mainly), over github' ohm and other resources with code,
and as it turned out, everything intersects perfectly and works).

Y
Yerlan Ibraev, 2020-11-17
@mad_nazgul

1) Spring doesn't start anything by itself - it's a framework.
2) No need to jump from one lesson to another. You won't be able to cross a snake with a hedgehog
3) Learn maven.
4) If you want a quick start, then start with SpringBoot, it's easier. To start SpringBoot IDEA is not needed, everything is done within the maven framework.
And yes, use the official documentation.
Specifically here is the IDE choice page

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question