Tool version compatibility problem
Must read : Framework based programs run successfully , For JAR Package version , The correctness of the configuration file has strict requirements , Anything goes wrong , Will cause the framework program to fail . If you are learning this framework for the first time ,
Be sure to follow the instructions of the tutorial strictly , Completely imitate the operation , Until you successfully see the running effect . After the first success , Confidence , Ideas will have a better cushion , Then according to their doubts , stay “ success ” Make the desired changes and adjustments to your code , This can greatly save learning time , increase of efficiency ,
Don't change it at once , Create obstacles to your learning
Correct
Step 2 : Imitation and troubleshooting Step 3 : Create project springmvc Step 4 : Import jar Bag Step 5 : web.xml Step 6 : establish springmvc-servlet.xml Step 7 : control class IndexController Step 8 : prepare index.jsp Step 9 : Deployed in tomcat In , Restart test Step 10 : schematic diagram Step 11 : Complete project download Step 12 : practice
Spring MVC A lot of steps need to be done , Any step missed , Did something wrong , Can fail , This will affect the confidence of learning , And mistakenly think that this tutorial is impassable .
So first download the runnable project in the upper right corner springmvc.rar, Unzip and import to eclipse In , Start Tomcat, Observe for normal operation . Make sure you can run , Make sure the tutorial can run , Learn the following . Import to Eclipse For the method of and operation in, please refer to : Import dynamic Web Project to Eclipse In After successful deployment , Test address , You should see the effect shown in the figure http://127.0.0.1:8080/springmvc/index notes : This is a dynamic project format , I won't support it independent Tomcat Mode deployment
After ensuring that the runnable project can run correctly , Then strictly follow the steps in the tutorial , Imitate the code again .
The imitation process inevitably has code differences , As a result, the expected operation results cannot be obtained , At this moment, by comparison The correct answer ( Runnable project ) And your own code , To locate the problem . In this way , Learning is effective , Troubleshooting is efficient , It can obviously improve the learning speed , Across all the barriers on the way to learning . It is recommended to use diffmerge Software , Compare folders . Put your own project folder , Compare with my runnable project folder . This software is awesome , You can know which two files in the folder are wrong , And clearly marked Here is a green installation and use tutorial : diffmerge Download and use tutorials
stay eclipse New item in springmvc, use dynamic web project The way . Students who are not familiar with this way , Please refer to
use Dynamic Web Project Develop in a way J2EE application
Download the... In the upper right corner lib.rar, Unzip and copy to e:/project/springmvc/WebContent/WEB-INF/lib Under the directory
stay WEB-INF Create... Under the directory web.xml
to configure Spring MVC Entrance to DispatcherServlet, Submit all requests to the Servlet be careful :<servlet-name> springmvc</servlet-name> springmvc This name will be used in the next step
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
stay WEB-INF Create... Under the directory springmvc-servlet.xml
springmvc-servlet.xml Same as in the previous step <servlet-name>springmvc</servlet-name> springmvc corresponding This is Spring MVC of Mapping profile Indicates the access path /index I'll give it to you id=indexController of bean handle id=indexController of bean Configure as class : IndexController
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> </beans>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> </beans>
control class IndexController Implementation interface Controller , Provide method handleRequest Processing requests
SpringMVC Through ModelAndView Objects combine models and views ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC"); Indicates that the view is index.jsp Model data is message, The content is “Hello Spring MVC”
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
stay WebContent Create... Under the directory index.jsp
index.jsp It's simple , Through EL expression Show message The content of
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <h1>${message}</h1>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <h1>${message}</h1>
Deployed in Tomcat In , Restart tomcat, Then visit the address , Observation effect
http://127.0.0.1:8080/springmvc/index For deployment methods, please refer to Through Eclipse Start Tomcat-Run On Server
1. User access /index
2. According to web.xml Configuration in All visits will go through DispatcherServlet 3. According to According to the configuration file springmvc-servlet.xml , Access path /index Will enter IndexController class 4. stay IndexController Jump to the page specified in index.jsp, And pass message data 5. stay index.jsp Display in message Information
Generally speaking , Do it step by step , You can run and see the results .
If you really can't see the results , Mostly because one of the steps in the middle is not careful enough , Case error , More underline and other subtle errors . If so , You can also download the complete project on the right to ensure that it can go through
This example implements the access path /index, The server jumps to index.jsp Effect of
practice : Access path /hello The server jumps to hello.jsp
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2021-10-11
bean Inside id It seems that you can successfully visit the page if you fill in it casually
1 One answer
Thousands of evening stars Jump to the problem location Answer time :2021-10-26
It should be equivalent to taking an alias , But the following path can't go wrong
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-09-25
springmvc Do not quote jstl Tag library
1 One answer
mumu123123 Jump to the problem location Answer time :2021-10-11
I don't want it any more ,eclipse Automatically imported
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2021-09-07
use idea My friends can view this blog
2021-07-30
Class not found
2021-03-31
Resource not found
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 128 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|