Hello World Application in Spring MVC framework Using Technologies Maven, Eclipse and JBoss

To begin with, download the JBoss in .zip file format.


Then go the folder "D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin" open CMD prompt and add an admin for the JBoss:

Check these logs:

  • D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin>add-user.bat

    What type of user do you wish to add?
     a) Management User (mgmt-users.properties)
     b) Application User (application-users.properties)
    (a): a

    Enter the details of the new user to add.
    Using realm 'ManagementRealm' as discovered from the existing property files.
    Username : tiger
    Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.
     - The password should be different from the username
     - The password should not be one of the following restricted values {root, admin, administrator}
     - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
    Password :
    Re-enter Password :
    What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]:
  • About to add user 'tiger' for realm 'ManagementRealm'
  • Is this correct yes/no? yes
  • Added user 'tiger' to file 'D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\standalone\configuration\mgmt-users.properties'
  • Added user 'tiger' to file 'D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\domain\configuration\mgmt-users.properties'
  • Added user 'tiger' with groups  to file 'D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\standalone\configuration\mgmt-groups.properties'
  • Added user 'tiger' with groups  to file 'D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\domain\configuration\mgmt-groups.properties'
  • Is this new user going to be used for one AS process to connect to another AS process?
  • e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
  • yes/no? yes
  • To represent the user add the following to the server-identities definition <secret value="TWFuYWdlckAxMjM=" />
  • Press any key to continue . . .
  • D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin>
If you are using JDK 11, you might get the error:
"org.jboss.modules.ModuleNotFoundException: java.se"

To overcome this, you have to add this line "--add-modules java.se" in the 'standalone.bat' as shown below:



To start the server, launch file "standalone.bat" from the path "D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin":

See these logs:

D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin>standalone.bat
  • Calling "D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\bin\standalone.conf.bat"
  • Setting JAVA property to "D:\ProgramFiles\Java\jdk1.8.0_31\bin\java"
  • ==================================================================
  • JBoss Bootstrap Environment

      JBOSS_HOME: "D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2"

      JAVA: "D:\ProgramFiles\Java\jdk1.8.0_31\bin\java"

      JAVA_OPTS: "-client -Dprogram.name=standalone.bat -Xms1G -Xmx1G -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -Xloggc:"D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\standalone\log\gc.log" -XX:GCLogFileSize=3M -XX:-TraceClassUnloading"

    ==================================================================
  • 05:17,003 INFO  [org.jboss.modules] (main) JBoss Modules version 1.8.5.Final-redhat-1
... ... ...
22:05:30,580 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
...

22:05:32,845 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443
 

22:05:33,470 INFO  [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBossWS 5.2.1.Final-redhat-1 (Apache CXF 3.2.5.redhat-00001)
 

22:05:33,611 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server
 

22:05:33,611 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
 

22:05:33,611 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
 

22:05:33,611 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: JBoss EAP 7.2.0.Beta (WildFly Core 5.0.5.Final-redhat-00001) started in 17405ms - Started 290 of 511 services (308 services are lazy, passive or on-demand) 

To check if JBoss is running, launch this URL "http://127.0.0.1:8080/" (it is taken from the logs above) in the browser:



Download Eclipse from this link (make sure that your JDK and Eclipse are both of the same version (either both are of 32-bit or both are of 64-bit)):
https://www.eclipse.org/photon/

Once it is set up, we start coding by creating a new Maven project.


Our project name is: "helloworld_using_spring_mvc"


1) Now, edit the dependencies in the 'pom.xml' file using the code below:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   
    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>servlet-api</artifactId> 
        <version>3.0-alpha-1</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
        <version>3.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
        <version>3.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-beans</artifactId> 
        <version>3.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-webmvc</artifactId> 
        <version>3.1.2.RELEASE</version> 
    </dependency> 

2) Now we create the request page:

Add this code to the index.jsp page:

<a href="hello.do">click</a>  

 3) Create the controller class

To create the controller class, we are using two annotations @Controller and @RequestMapping.
The @Controller annotation marks this class as Controller.
The @Requestmapping annotation is used to map the class with the specified name.
This class returns the instance of ModelAndView controller with the mapped name, message name and message value. The message value will be displayed in the jsp page.

package com.javatpoint; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 
@Controller 
public class HelloWorldController { 
    @RequestMapping("/hello") 
    public ModelAndView helloWorld() { 
        String message = "HELLO SPRING MVC!"; 
        return new ModelAndView("hellopage", "message", message); 
    } 


4) Provide the entry of controller in the web.xml file

In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request of the '.do' type will be forwarded to the DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>       
    <web-app version="2.5"  
        xmlns="http://java.sun.com/xml/ns/javaee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
     <servlet> 
        <servlet-name>spring</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>spring</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
    </web-app>  


5) Define the bean in the XML file 

This is the important configuration file where we need to specify the ViewResolver and View components.
The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.
Here, the InternalResourceViewResolver class is used for the ViewResolver.
The prefix+string returned by controller+suffix page will be invoked for the view component.
This xml file should be located inside the WEB-INF directory.
"helloworld_using_spring_mvc/src/main/webapp/WEB-INF"

    <?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:p="http://www.springframework.org/schema/p" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
        <context:component-scan  base-package="com.javatpoint" /> 
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
            <property name="prefix" value="/WEB-INF/jsp/" /> 
            <property name="suffix" value=".jsp" /> 
        </bean> 
    </beans>  


6) Display the message in the JSP page

This is the simple JSP page, displaying the message returned by the Controller.
It must be located inside the WEB-INF/jsp directory for this example only.

Message is: ${message}  

7) Deploying the application

To deploy the project on JBoss, we export it in the WAR format using the context menu as shown below:

Apart from context menu, important to note here is the "directory structure" for the project:
Make sure your .java file is in the correct directory and if it is not, you create it using the Windows File Explorer (the path is: D:\Workspace\Eclipse Workspace\helloworld_using_spring_mvc\src\main\java\com\javatpoint)



Now, we take our WAR file "helloworld_using_spring_mvc.war" and paste at the path:
"D:\ProgramFiles\jboss-eap-7.2.0.Beta\jboss-eap-7.2\standalone\deployments"

8) Launching the application
The URL for the application is: http://localhost:8080/helloworld_using_spring_mvc/


9) See the output on the click of the button:


References: https://www.javatpoint.com/spring-mvc-tutorial

No comments:

Post a Comment