1. [machine] using Spring MVC achieve helloworld function
2. [machine] using annotations Spring MVC way to achieve the function helloworld
<?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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd " > ! <- 1. Configuration annotation -> < context: Annotation-config > </ context: Annotation-config > < context: Scan-Component Base-Package = "renjiale" > </ context: Component-Scan > <-! 2.MVC movable with annotations -> < MVC: annotation-Driven > </ MVC: annotation-Driven > <! - configuration viewresovler view handler -> <! - REST style: http: // domain /index.jsp -> < bean the above mentioned id = "myspringmvc" class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > < Property name = "prefix" value = "/" > </ Property > < Property name = "suffix" value =. "JSP" > </ Property > <! - to find the page corresponding prefixes and suffixes, the OK -> </ the bean > <! - 4. write code -> <! - the configuration dispatcherServlet (MVC) auto-configuration; in web.xml Configuring the servlet -> <-! 6. write the servlet -> <bean id="dispatcherServlet" class= "org.springframework.web.servlet.DispatcherServlet" > </ the bean > <-! 7. The page write tests -jsp -> <-! 8.MVC processing request, write traffic classes -> <! - 9. when .jsp click request, so that the traffic class methods to handle the request -> <-! 10.controller implement the controller using the annotation -> <-! 11.jsp plurality of service requests to find method @RequestMapping find the corresponding class @RequestMapping -> <-! 12. When a request to jump to the designated place .jsp pages -> </ Beans >
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>web_cookie</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>mvc01</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mvc01</servlet-name> <!-- 注: 所有请求MVC来处理 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> <h1> Here index.jsp page </ h1 of > < A the href = "MAV / Show" > Test </ A > <-! The MVC processing request -> </ body > </ HTML >
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> <h1> Here is the sucess page </ h1 of > </ body > </ HTML >
Package Penalty for renjiale.handleRequest; Import org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.servlet.ModelAndView; / * * Business class: * Note: not inherit his father class does not implement interface * / @Controller @RequestMapping ( "/ MAV" ) public class MyFirstMVC { // traffic processed, need to return ModelAndView object @RequestMapping (value = "Show" ) public ModelAndView Show () { the System.out. the println ( "Show method performs" ); ModelAndView MAV =new ModelAndView(); mav.setViewName("sucess"); return mav; } }