SpringMVC annotation development entry, springmvc annotation entry
1. Introduction to Spring MVC Framework
- Supports restful URLs
- Add more annotations to fully annotate the driver
- Introduce the HTTP input/output converter (HttpMessageConverter)
- Seamless integration with data conversion, formatting, and verification frameworks
- Special support for static resource processing
- More flexible Controller Method signature, which can be completely independent from Servlet API
2. Spring MVC Framework Structure and execution process
3. How to Use Spring-MVC in applications?
4. Let's take a look at the small example of annotation Development (simple logon)
I. forward)
Source code introduction:
1. lib package (jar package) and web. xml file configuration
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
View Code
2. index. jsp (log on to the homepage)
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> View Code
3. hello. jsp (the page to jump to after successful logon) and error. jsp (the page to jump to when logon fails)
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> View Code
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> View Code
4. MyController. java (define your own processor)
Package cn. zhang. controller; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; // define your processor @ Controllerpublic class MyController {@ RequestMapping (value = "/frist. do ", produces =" text/html; charset = UTF-8 ", method = RequestMethod. POST) public String frist (Model model, String name, int age) {model. addAttribute ("name", name); model. addAttribute ("age", age); System. out. println (name); System. out. println (age); return "forward:/hello. jsp ";}}View Code
5. applicationContext. xml (Spring configuration file)
<? 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" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Use annotation to complete ing --> <context: component-scan base-package = "cn. zhang. controller"> </context: component-scan> <! -- Mvc annotation driver --> <mvc: annotation-driven/> </beans>
View Code
Effect display:
1. Successful Input
Click Submit to go to the success page:
If you enter other information, the error page is displayed:
2. Redirection (redirect)
We just need to change our controller.
Package cn. zhang. controller; import org. springframework. stereotype. controller; import org. springframework. ui. model; import org. springframework. web. bind. annotation. requestMapping; // define your processor @ Controllerpublic class MyController {@ RequestMapping (value = "/frist. do ") // the login request is frist. do public String frist (Model model, String name, int age) {return "redirect: hello. do "; // redirect to hello. do} @ RequestMapping (value = "/hello. do ") public String hello (Model model, String name, int age) {model. addAttribute ("name", name); model. addAttribute ("age", age); System. out. println (name); System. out. println (age); if (name. equals ("1") & age = 1) {return "redirect:/hello. jsp "; // redirect to hello. jsp} else {return "redirect:/error. jsp "; // redirect to error. jsp }}}View Code