Jsp Web Content Folder Uploaded Image Some Not Showing Up Get 404 Errot
Model-View-Controller (MVC) architecture provides the simplest mode to develop flexible and loosely coupled web applications.
In this tutorial we will go over on how to create your 1st Jump MVC application with very detailed steps.
Do you lot accept whatsoever of below questions?
- Fundamentals of Spring MVC 5.two.iii
- Developing a Spring Framework MVC v application step-past-step..
- coffee – Spring MVC tutorial from the scratch
- Jump MVC Fast Tutorial
- Spring MVC Framework Tutorial
- Kickoff Spring MVC application tutorial
- Spring 5.ten MVC Tutorials, AJAX Demo, jQuery Demo, JavaScript Demo, Tips & Tricks Spring 5 MVC
Then you lot are at correct place. Here I'll demonstrate simpleSpring MVC framework for edifice web applications.
First matter starting time – Let's Setup Environment
Tutorial final updated and verified: September 2020 with below Tool versions.
I'm using below tools which you may need to download if you don't have already.
- Tomcat ix.0.38 – Download latest Apache Tomcat from this link.
- Brand certain you download Eclipse IDE for
Java EEDevelopers (2020‑09 R) – Download link. (diagram below) -
Spring 5.2.9.RELEASE(No download required) – we will apply Maven dependency. - JDK xv – Download link.
Make sure you lot download latest Java EE Developer Kit:
Principal Goal
Main goal for this tutorial to create Spring MVC Application in the simplest fashion.
Before we get started let'south run across what we will see at the cease of this tutorial 🙂
This is how our application result volition look like. This is a final outcome once you complete all below steps.
Here is a final result: Welcome page ==> alphabetize.jsp
Result returns from Controller Class 🙂
Now Let'south get started on Tutorial
Footstep-1
- Open Eclipse
- Create
New Eclipse Workspace– This ismustto avoid any existing workspace config issue.
Step-ii
- Click on
File - Click on
New - Cull
Dynamic Web Project - One popup window, Provide Project Name:
CrunchifySpringMVCTutorial - Make sure you utilise
Target RuntimeasApache Tomcat ix.0- If you don't see Target Runtime and then follow these steps
- Choose Configuration as
Default Configuration
Step-3
Catechumen Projection to Maven Project to add all required Spring MVC dependencies to project.
Steps:
- Right click on project
- Configure
- Catechumen to
Mavenprojection
Footstep-iv
Open pom.xml file and add below jar dependencies to project.
NOTE:Here is mypom.xmlfile. Make sure you lot update Java version to13if you lot oasis't yet moved toJDK xiii. We will keep updating this tutorial to latest Spring MVC version.So below pom.xml file may have different (latest) version of Jump MVC dependencies than above image 🙂
| 1 2 iii iv 5 6 7 8 9 10 11 12 13 xiv 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | < project xmlns="http://maven.apache.org/POM/4.0.0" xmlns : xsi="http://www.w3.org/2001/XMLSchema-case" xsi : schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > four.0.0 </modelVersion > < groupId > CrunchifySpringMVCTutorial </groupId > < artifactId > CrunchifySpringMVCTutorial </artifactId > < version > 0.0.1-SNAPSHOT </version > < packaging > war </packaging > < build > < sourceDirectory > src </sourceDirectory > < plugins > < plugin > < artifactId > maven-compiler-plugin </artifactId > < version > 3.8.0 </version > < configuration > < release > 13 </release > </configuration > </plugin > < plugin > < artifactId > maven-war-plugin </artifactId > < version > three.two.i </version > < configuration > < warSourceDirectory > WebContent </warSourceDirectory > </configuration > </plugin > </plugins > </build > < dependencies > < dependency > < groupId > org . springframework </groupId > < artifactId > jump-core </artifactId > < version > 5.2.3.RELEASE </version > </dependency > < dependency > < groupId > org . springframework </groupId > < artifactId > spring-context </artifactId > < version > v.2.three.RELEASE </version > </dependency > < dependency > < groupId > org . springframework </groupId > < artifactId > spring-aop </artifactId > < version > 5.2.3.RELEASE </version > </dependency > < dependency > < groupId > org . springframework </groupId > < artifactId > bound-webmvc </artifactId > < version > five.2.3.RELEASE </version > </dependency > < dependency > < groupId > org . springframework </groupId > < artifactId > leap-web </artifactId > < version > five.two.3.RELEASE </version > </dependency > < dependency > < groupId > javax . servlet </groupId > < artifactId > jstl </artifactId > < version > 1.2 </version > </dependency > </dependencies > </project > |
Step-5
Create new Leap Configuration Bean file:/WebContent/Spider web-INF/crunchify-servlet.xml
crunchify-servlet.xml
| 1 2 3 4 5 6 7 8 ix 10 11 12 13 14 xv 16 17 18 19 20 21 22 23 24 25 26 27 | <? xml version="i.0" encoding="UTF-8" ?> < beans xmlns="http://world wide web.springframework.org/schema/beans" xmlns : mvc="http://www.springframework.org/schema/mvc" xmlns : context="http://www.springframework.org/schema/context" xmlns : xsi="http://world wide web.w3.org/2001/XMLSchema-instance" 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" > < mvc : annotation-driven /> < context : component-scan base-packet="com.crunchify.controller" /> < mvc : default-servlet-handler /> < bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" > < property proper name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> < property proper noun="prefix" value="/Web-INF/jsp/" /> < holding name="suffix" value=".jsp" /> </bean > </beans > |
In the above crunchify-servlet.xmlconfiguration file, we have defined a tag <context:component-scan> . This volition allow Leap to load all the components from packagecom.crunchify.controller and all its child packages.
This will load our CrunchifyHelloWorld.form . Also we have defined a bean viewResolver. This bean will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView.
Note that in our CrunchifyHelloWorld class, we have return a ModelAndView object with view name welcome.
This will be resolved to path /Web-INF/jsp/welcome.jsp .
Step-6
Create new file web.xml if it'south already non there. Map Jump MVC in/WebContent/Spider web-INF/web.xml file.
NOTE: if yous don't see web.xml file in your "dynamic web projection" then follow these steps.
web.xml
| 1 2 3 4 v half dozen 7 eight nine 10 11 12 13 14 fifteen sixteen 17 18 nineteen 20 21 22 23 24 25 26 27 28 | <? xml version="one.0" encoding="UTF-8" ?> < web-app xmlns : xsi="http://www.w3.org/2001/XMLSchema-example" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi : schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0" > < brandish-name > CrunchifySpringMVCTutorial </display-name > < welcome-file-list > < welcome-file > alphabetize . html </welcome-file > < welcome-file > index . htm </welcome-file > < welcome-file > alphabetize . jsp </welcome-file > < welcome-file > default . html </welcome-file > < welcome-file > default . htm </welcome-file > < welcome-file > default . jsp </welcome-file > </welcome-file-list > < servlet > < servlet-name > crunchify </servlet-name > < servlet-class > org . springframework . web . servlet . DispatcherServlet </servlet-form > < load-on-startup > 1 </load-on-startup > </servlet > < servlet-mapping > < servlet-proper name > crunchify </servlet-name > < url-pattern >/welcome . jsp </url-pattern > < url-pattern >/index . jsp </url-design > < url-blueprint >/welcome . html </url-blueprint > < url-blueprint >*. html </url-blueprint > </servlet-mapping > </web-app > |
The above code in spider web.xml will map DispatcherServlet with url pattern /welcome.jsp. Besides note that we accept define index.jsp as welcome file.
Ane affair to annotation here is the name of servlet in <servlet-proper name> tag in web.xml. One time the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml in WEB-INF folder of web application. In this example, the framework volition look for file called crunchify-servlet.xml.
Pace-7
Create Controller Class.
- Right click on
Java Resources->src - Click
New->Course - Package:
com.crunchify.controller - Filename:
CrunchifyHelloWorld.coffee
CrunchifyHelloWorld.java
| 1 2 3 4 5 vi 7 8 9 10 xi 12 13 xiv 15 16 17 eighteen 19 twenty 21 22 | package com . crunchify . controller ; import org . springframework . stereotype . Controller ; import org . springframework . web . bind . annotation . RequestMapping ; import org . springframework . spider web . servlet . ModelAndView ; /* * author: Crunchify.com * */ @Controller public grade CrunchifyHelloWorld { @RequestMapping ( "/welcome" ) public ModelAndView helloWorld ( ) { String message = "<br><div style='text-align:middle;'>" + "<h3>********** Hello Globe, Leap MVC Tutorial</h3>This message is coming from CrunchifyHelloWorld.java **********</div><br><br>" ; return new ModelAndView ( "welcome" , "message" , bulletin ) ; } } |
Note that nosotros accept annotated the CrunchifyHelloWorld class with @Controller and @RequestMapping("/welcome"). When Spring scans our parcel, information technology will recognize this edible bean equally being a Controller bean for processing requests. The @RequestMapping notation tells Jump that this Controller should procedure all requests get-go with /welcome in the URL path. That includes /welcome/* and /welcome.html.
The helloWorld() method returns ModelAndView object. The ModelAndView object tries to resolve to a view named "welcome" and the data model is beingness passed back to the browser so we can access the data within the JSP. The logical view proper noun will resolve to /WEB-INF/jsp/welcome.jsp . Logical name "welcome" which is return in ModelAndView object is mapped to path /WEB-INF/jsp/welcome.jsp.
The ModelAndView object likewise contains a message with fundamental "message" and Detailed value. This is the data that we are passing to our view. Unremarkably this will exist a value object in grade of java bean that will incorporate the data to exist displayed on our view. Here we are simply passing a cord.
Step-8
The View –Create new file/WebContent/index.jsp.
index.jsp
| ane 2 3 4 five 6 seven 8 9 x eleven 12 thirteen 14 15 16 17 18 nineteen 20 21 22 | < html > < head > < championship > Bound MVC Tutorial Serial by Crunchify . com </championship > <style type ="text/css"> torso { background-image : url ( 'https://cdn.crunchify.com/bg.png' ) ; } </way> </head > < body > < br > < div style="text-marshal: heart" > < h2 > Hey Yous . . ! ! This is your 1st Spring MCV Tutorial . . < br > < br > </h2 > < h3 > < a href="welcome.html" > Click here to See Welcome Message . . . </a > ( to check Spring MVC Controller . . . @ RequestMapping ( "/welcome" ) ) </h3 > </div > </body > </html > |
Create some other file/WebContent/WEB-INF/jsp/welcome.jsp.
NOTE: Don't forget to create jsp folder and put welcome.jsp inside that 🙂
welcome.jsp
| 1 2 3 iv 5 vi seven 8 9 x xi 12 xiii fourteen 15 xvi 17 18 19 20 21 22 23 24 25 | < html > < head > < title > Jump MVC Tutorial by Crunchify - Hello World Spring MVC Example </championship > <way type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/bg.png' ) ; } </way> </caput > < body > $ { message } < br > < br > < div style="font-family unit: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align: center;" > Spring MCV Tutorial by < a href="https://crunchify.com" > Crunchify </a > . Click < a href="https://crunchify.com/category/coffee-tutorials/" target="_blank" > here </a > for all Java and < a href='https://crunchify.com/category/leap-mvc/' target='_blank' > here </a > for all Spring MVC , Web Development examples . < br > </div > </body > </html > |
After everything this is how your workspace should look similar.
Step-9
Right Click on Project -> Run As -> Maven Build...
Add Goals: clean install. Click Apply and Run.
Yous should see build success message:
Where are all of my .jar files?
You volition run across all .jar files nether /target folder. Screenshot.
Footstep-10
- If you
don't encounterTomcat Server inServerstab then follow steps to add Apache Tomcat to Eclipse. - Deploy project to
Apache Tomcat- Correct click
- Add and Remove
- Add Project to server (correct side section).
- Click on Start.
Make sure you come across below logs. That means your awarding is successfully deployed on Tomcat Web Server.
| 1 2 three 4 v vi 7 8 9 10 11 12 13 fourteen 15 sixteen 17 xviii nineteen twenty 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | Aug 04 , 2018 9 : 08 : ten PM org . apache . tomcat . util . digester . SetPropertiesRule begin Alert : [ SetPropertiesRule ] { Server/Service/Engine/Host/Context } Setting holding 'source' to 'org.eclipse.jst.jee.server:CrunchifySpringMVCTutorial' did not find a matching property . INFO : Server version : Apache Tomcat/9.0.x INFO : Server built : Jun 20 2018 17 : 32 : 21 UTC INFO : Server number : 9.0.ten.0 INFO : OS Name : Mac OS X INFO : OS Version : 10.13.6 INFO : Architecture : x86_64 INFO : Java Dwelling : /Library/Java/JavaVirtualMachines/jdk-x.0.two.jdk/Contents/Home INFO : JVM Version : 10.0.2+13 INFO : JVM Vendor : "Oracle Corporation" INFO : CATALINA_BASE : /Users/appshah/Documents/jee-photon/workspace/c/. metadata/. plugins/org . eclipse . wst . server . core/tmp0 INFO : CATALINA_HOME : /Users/appshah/Documents/jee-photon/apache-tomcat-9.0.10 INFO : Control line argument : -Dcatalina . base=/Users/appshah/Documents/jee-photon/workspace/c/. metadata/. plugins/org . eclipse . wst . server . core/tmp0 INFO : Control line argument : -Dcatalina . habitation=/Users/appshah/Documents/jee-photon/apache-tomcat-9.0.ten INFO : Command line argument : -Dwtp . deploy=/Users/appshah/Documents/jee-photon/workspace/c/. metadata/. plugins/org . eclipse . wst . server . cadre/tmp0/wtpwebapps INFO : Command line argument : -Dfile . encoding=UTF-8 INFO : The April based Apache Tomcat Native library which allows optimal operation in production environments was non found on the java . library . path : [/Users/appshah/Library/Java/Extensions :/Library/Coffee/Extensions :/Network/Library/Coffee/Extensions :/Organisation/Library/Java/Extensions :/usr/lib/coffee : . ] INFO : Initializing ProtocolHandler [ "http-nio-8080" ] INFO : Using a shared selector for servlet write/read INFO : Initializing ProtocolHandler [ "ajp-nio-8009" ] INFO : Using a shared selector for servlet write/read INFO : Initialization candy in 841 ms INFO : Starting service [ Catalina ] INFO : Starting Servlet Engine : Apache Tomcat/9.0.10 INFO : At to the lowest degree ane JAR was scanned for TLDs all the same contained no TLDs . Enable debug logging for this logger for a complete listing of JARs that were scanned but no TLDs were found in them . Skipping unneeded JARs during scanning tin can improve startup time and JSP compilation fourth dimension . INFO : At to the lowest degree one JAR was scanned for TLDs yet contained no TLDs . Enable debug logging for this logger for a complete list of JARs that were scanned only no TLDs were institute in them . Skipping unneeded JARs during scanning tin better startup fourth dimension and JSP compilation fourth dimension . INFO : No Spring WebApplicationInitializer types detected on classpath INFO : Initializing Spring FrameworkServlet 'crunchify' INFO : FrameworkServlet 'crunchify' : initialization started INFO : Refreshing WebApplicationContext for namespace 'crunchify-servlet' : startup engagement [ Sabbatum Aug 04 21 : 08 : 13 CDT 2018 ] ; root of context bureaucracy INFO : Loading XML bean definitions from ServletContext resource [/Web-INF/crunchify-servlet . xml ] INFO : Mapped "{[/welcome]}" onto public org . springframework . web . servlet . ModelAndView com . crunchify . controller . CrunchifyHelloWorld . helloWorld ( ) INFO : Looking for @ ControllerAdvice : WebApplicationContext for namespace 'crunchify-servlet' : startup appointment [ Sat Aug 04 21 : 08 : 13 CDT 2018 ] ; root of context hierarchy INFO : Looking for @ ControllerAdvice : WebApplicationContext for namespace 'crunchify-servlet' : startup date [ Sat Aug 04 21 : 08 : 13 CDT 2018 ] ; root of context hierarchy INFO : Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' INFO : FrameworkServlet 'crunchify' : initialization completed in 1607 ms INFO : Starting ProtocolHandler [ "http-nio-8080" ] INFO : Starting ProtocolHandler [ "ajp-nio-8009" ] INFO : Server startup in 3579 ms |
Step-11. Done.
Visit: http://localhost:8080/CrunchifySpringMVCTutorial/ and you should be all gear up.
Hurray.. At present y'all know Hi Earth Bound MVC v Example. Allow me know if you come across any exception while running this. There are lot more than example you tin can find here.
Practice you desire to include JS, CSS and images into JSP file? Follow this tutorial: Best way to Add together/Integrate JS, CSS and images into JSP file using 'mvc:resources mapping'.
Having trouble? Any issue?
Triaging Pace-ane – Having HTTP Status 404 fault?
Likewise, follow below tutorial:
Triaging pace-2 – URL doesn't work? Tomcat mistake?
Make sure you lot add together Apache Tomcat Server to Targeted Runtime. Which you may accept selected in Pace-1. Tomcat 7 or 8 any – server should piece of work.
Triaging Step-3 – maven errors?
Brand sure to update all maven dependencies.
Experience free to e-mail or comment below if you have any problem running above tutorial.
Source: https://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/
Post a Comment for "Jsp Web Content Folder Uploaded Image Some Not Showing Up Get 404 Errot"