A simple tutorial on getting started
The source code of this instance and the jar in the instance
Source code: http://xiazai.VeVB.COM/201612/yuanma/SpringMVC_jb51.zip
Required jar: http://xiazai.VeVB.COM/201612/yuanma/SpringMVCjar_jb51.zip
Another article about SpringMVC file upload, multi-file upload://www.VeVB.COM/article/100491.htm
Examples of simple annotation configuration:
1. Create a project:
1. Create a new dynamic web project:
2. Name the project: SpringMVC_01
3. Add tomcat runtime environment/dependence library. If it is MyEclipse, this step is not required when creating a web project.
Right-click the project and click Build Path->Add Librarys:
After adding, there will be additional tomcat Servlet packages
4. Finally, add the jars required for Spring and SpringMVC, and I add the following jars to the project
2. Configuration file:
1. First configure a DispatcherServlet in web.xml and specify the URL that needs to be intercepted through <servlet-mapping>. The following xml configures an intercepting url with suffixed.html.
<!-- Configure Spring MVC DispatcherServlet --> <servlet> <servlet-name>MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Initialization parameters --> <init-param> <!-- Load the SpringMVC xml into the spring context container --> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/mvc*.* </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- The url that needs to be intercepted by configuring the DispatcherServlet --> <servlet-mapping> <servlet-name>MVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
First configure a servlet and then load the SpringMVC xml file into the Spring context. Then configure servlet-mapping, servlet-name is the configured name in the servlet just now, and then specify that the url to be intercepted is *.html
2. Configure Spring's context listener and specify the path to Spring's xml configuration file.
<!-- Listen to the spring context container --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Load the spring xml configuration file into the spring context container --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:root-context.xml</param-value> </context-param>
The path classpath specified here is in the classpath file after the project is compiled.
Final web.xml file content:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- Listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Loading the spring xml configuration file into the spring context container --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:root-context.xml</param-value> </context-param> <!-- Configure Spring MVC DispatcherServlet --> <servlet> <servlet-name>MVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Initialization parameters --> <init-param> <!-- Load the SpringMVC xml into the spring context container --> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/mvc*.* </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- The url that needs to be intercepted by configuring the DispatcherServlet --> <servlet-mapping> <servlet-name>MVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
3. Create the xml file required for SpringMVC and the xml file of applicationContext. Here, since the format required for init-param in the servlet configured in the first step is: mvc*.*. It is to find a file starting with mvc, so when creating the xml file of SpringMVC, I must start with mvc. I named it: mvc-context.xml, and according to the configuration in context-param, name the applicationContext file: root-context.xml;
4. Configure mvc-context.xml:
First import root-context.xml through the import tag, and then scan the specified package name through the component-scan tag to make the spring annotation of all Java classes under the package take effect.
Then configure SpringMVC's view rendering parser, so that its prefix is /page/ and its suffix is .jsp, so that the paths that SpringMVC needs to render can be found in /page/return value.jsp.
<!-- Load Spring's global configuration file --> <beans:import resource="root-context.xml" /> <!-- SpringMVC configuration --> <!-- Let Spring scan all classes under org.swinglife.controller through component-scan, so that Spring's code annotations take effect --> <context:component-scan base-package="org.swinglife.controller"></context:component-scan> <!-- Configure SpringMVC's view renderer, with its prefix as:/page/ and its suffix as .jsp Render the view to /page/<method return value>.jsp--> <beans:bean p:prefix="/page/" p:suffix=".jsp"> </beans:bean>
Finally, mvc-context.xml and root-context.xml are:
mav-context.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Load Spring's global configuration file --> <beans:import resource="root-context.xml" /> <!-- SpringMVC configuration --> <!-- Let Spring scan all classes under org.swinglife.controller through component-scan to make Spring's code annotations take effect --> <context:component-scan base-package="org.swinglife.controller"></context:component-scan> <!-- Configure SpringMVC's view renderer, with its prefix:/ with a suffix. Render the view into /page/<method return value>.jsp--> <beans:bean p:prefix="/page/" p:suffix=".jsp"> </beans:beans>
root-context.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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans>
3. Write Controller
1. Create org.swinglife.controller package to store the Controller class, and then create a new HomeController.java to write the Controller on the homepage
2. Use the annotation @Controller to define the HomeController class as a Controller, and use @RequestMapping("value") to specify the path or method name to access in the method. SpringMVC can convert a POJO into a controller that handles the request through a @Controller annotation, and specify which requests are required for the controller through @RequestMapping.
@Controller public class HomeController { /*** * Home page Return to /page/home.jsp page* @return */ @RequestMapping("index") public ModelAndView index(){ //Create model and view to render the page. And specify that the page to be returned is the home page ModelAndView mav = new ModelAndView("home"); return mav; } }The ModelAndView object is defined in the method. Through this object, the view to be rendered is specified as home and finally returns ModelAndView. Render the page into home.jsp.
3. Finally, create /page/home.jsp in the WebContent directory so that SpringMVC can find and render the page view.
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>home</title> </head> <body> <h2>spring mvc instance</h2> </body> </html>
Now a complete SpringMVC mode has been built, you can run the project for testing.
4. Write the submission and delivery of parameters:
1. Write a new UserController class to assume that the user is logged in, submit username and password to the Controller for processing, and after logging in, the username and password are passed to the successful page.
Create UserController.java
Create /page/succ.jsp page as user successful login page
Code in UserController:
@Controller public class UserController { /*** * User login* <p>Annotation configuration, only POST is allowed to submit to this method* @param username * @param password * @return */ @RequestMapping(value="login",method=RequestMethod.POST) public ModelAndView login(String username,String password){ //Verify whether the passed parameters are correct, otherwise return to the login page. if(this.checkParams(new String[]{username,password})){ //Specify the page to be returned as succ.jsp ModelAndView mav = new ModelAndView("succ"); //Return the parameter to the page mav.addObject("username",username); mav.addObject("password", password); return mav; } return new ModelAndView("home"); } /*** * Verify whether the parameter is empty* @param params * @return */ private boolean checkParams(String[] params){ for(String param:params){ if(param==""||param==null||param.isEmpty()){ return false; } } return true; } First specify @Controller, and then specify @RequestMapping as login method;
It should be noted that this time, the page method mode is specified in @RequestMapping that the page method mode must be POST mode, otherwise it will not be accessible. Secondly, the value parameter specifies the access path.
And set the parameter in the login method, and the parameter is the name attribute in the form.
Then add parameters to the request through the addObject method of ModelAndView, so that these parameters can be displayed on the returned page.
There are other ways to pass parameters into the page in addition to this:
/*** * Another form of parameter passing* Process the requested parameters. * @param username * @param password * @param request * @return */ @RequestMapping(value="login",method=RequestMethod.POST) public ModelAndView login(String username,String password,HttpServletRequest request){ request.setAttribute("username", username); request.setAttribute("password", password); return new ModelAndView("succ"); }The above method is used directly by adding parameters to the request.
2. Write succ.jsp page and form page:
succ.jsp:
<body> <h2>Login</h2> username:${username } <p> password:${password } </body> form:<form action="login.html" method="post"> username:<input type="text" name="username" /> <p> password:<input type="password" name="password"/> <p> <input type="submit" value="submit" /> </form> 3. Finally run the project to test:
OK is all done. The above is a relatively simple example of SpringMVC.
In the given source code, there is another method to directly use String as the return value to specify the display page.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.