Springmvc learning notes first springmvc program

eclipse shortcuts:
Find file open type Ctrl + shift +t
Format code CTRL + SHIFT + F
Return to the previous edit position alt+ left keyboard key
Make large / small: Ctrl+Shift+X/Y
Code prompt alt+/
Eclipse tips:
Copy the full name of the class: open the class, select the class name, right-click and select copy qualified name, and then copy the complete class name.

SpringMVC learning notes:

1. required jar package: spring aop Jar spring bean Jar spring context Jar spring web Jar spring webmvc Jar spring core Jar
2. the first SpringMVC program
① : Springmvc configuration file: Springmvc XML
Open: src Java Right click resources - > other - > Spring – >Spring bean configuration file

② : select a common namespace: bean AOP context MVC

3. normal servlet process:
Request URL prtern to the corresponding servlet for processing
If you want to use springmvc instead of a normal servlet, how can you tell the program? To get springmvc involved in the program, you need to configure a servlet that comes with the springmvc jar package
Through the following configuration (web.xml: press alt+/ to jump out of the prompt, find the dispathcherservlet, modify param value and URL pattern), intercept all requests and submit them to springmvc for processing:

4. create a controller class:

5. configure springmvc XML (scanner: context and view parser: InternalResourceViewResolver):

<!-- Scanner. If you add annotations, you need to put the annotation package in the scanner -->
	<context:component-scan base-package="controller"></context:component-scan>
	<!-- Configure view parser (InternalResourceViewResolver)-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/"></property>
		<property name="suffix" value=".jsp"></property>

By the way, write springtmvc Basic configuration of XML:


6. write index JSP and helloworld JSP

7. on the web Configuring servlet s in XML

			<!-- classpath The corresponding path is actually the src In file -->
		<!-- Whether the tag container loads this at startup servlet
		When the value is 0 or greater than 0, it means that the container loads the servlet;
		When it is a negative number or is not specified, it indicates that the container servlet Load only when selected.
			The smaller the value of a positive number, the servlet The higher the priority of. -->
		<!-- servlet-name A random name -->
		<!-- Intercept all requests and give them to springmvc handle -->

Where URL pattern>/</url-pattern
/: all requests, note no/*
/User: block requests starting with /user
/User/abc Do: only block the request
Action: only intercept Request at the end of action

8. the HiddenHttpMethodFilter filter is added to add a put/delete request method to ordinary browsers

<!-- increase HiddenHttpMethodFilter Filter, the purpose is to add put/delete Request mode -->

9. the default page Jump is to request forwarding, and the address bar changes. If it is redirection, the address bar will not change

10. specify the request interception method through method

@RequestMapping(value=" hello",method=RequestMethod.POST,params={"name=zs"",age!=23","!sex"},headers={The request header must be the same as the one in order to be intercepted})

Params attribute value: the name parameter must be present, and name=zs; The age parameter does not have to be present. In some cases, it cannot be equal to 23; The sex parameter must not exist.
Headers attribute request headers for conventions
11.ant style request path
?: Single character
*: any character (0 or more)
**: any directory
12.jsp parameter transfer:

controller class receives:
Public String hello (@PathVariable("name") String name){
13.web XML configuration servlet path and servletMapping automatic generation method: alt+/, select dispatcherservlet dispatcherservlet deletion, and only need to modify it later


14.REST style: software coding style
Post: add
Get: query
Delete: delete
Put: Change
Ordinary browsers only support post and get methods. Other methods, such as delete/put requests, are newly added through a filter (HiddenHttpMethidFilter)
Filter criteria: hidden
2. the request method is post

For example:

<form action="hello" method ="post">
<!-- To be used delete and put Add a filter condition (filter hidden fields)-->
<input type=""hidden" name="_method" value="DELETE/PUT"/>
<input type ="submit" value ="Change ">

15.springmvc implementation: steps of put/delete request mode
A. On the web XML add filter:

<!-- increase HiddenHttpMethodFilter Filter, the purpose is to add put/delete Request mode -->

B. Forms
1. the request method is post
2 add hidden field

<input type="hidden" name="_method" value="DELETE/PUT"/>

C. Controller

	public String testAdd(@PathVariable("name") String name,@PathVariable("id") int id) {
		return "success";

Through method=requestmethod Delete/put/post/get matches the specific request method
In addition, it can be found that when the mapping names are the same, such as @RequestMapping(value="testRest"), different requests can be processed through the method to jump.

Part of the source code for processing put/delete requests in the filter:

	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		HttpServletRequest requestToUse = request;

		if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
			String paramValue = request.getParameter(this.methodParam);
		if (StringUtils.hasLength(paramValue)) {
				String method = paramValue.toUpperCase(Locale.ENGLISH);
				if (ALLOWED_METHODS.contains(method)) {
					requestToUse = new HttpMethodRequestWrapper(request, method);

		filterChain.doFilter(requestToUse, response);

Original request: request. By default, the request only supports POST, get, and header. However, if it is POST and has hidden fields,
, the filter adds the original request to the new request method delete, converts the original request to a requestToUse request (request), and finally puts the requestToUse into the request chain, and then uses the modified request

.@RequestParam("uname") String name @RequestParam(value="uid",required =false,defaultValue="98") Integer id

@RequestParam("uname"): accepts the value passed by the foreground, which is equivalent to request Getparameter ("uname";
Required=false: this attribute is not required. The default value is true
defaultValue= "99": default value

(Note: most of the values obtained through annotations are in this form. The value name eg:public String test(@CookieValue("cookie") String cookie) {} is stored through the @ annotation ("value name to get"))

17. get request header information:
Use the @RequestHeader() annotation to get
public String testRequestHeader(@RequestHeader("cookie") String cookie)
Obtain the value of the cookie in the request header through the @RequestHeader("cookie") String cookie, and save the value in the cookie

18. obtain cookie value through springmvc
@Cookievalue (when the server accepts the first request from the client, it will assign a session to the client (the session contains a sessionId), and when the server responds to the client for the first time, please assign the sessionid to JSESSIONID and pass the value to the sessionid of the client)
public String testCookieValue(@CookieValue("JSESSIONID") String jsessionid)

19. use object (entity class student) to receive request parameters

Springmvc processes / logic for handling various parameters:
Request: the front end sends a request a – > @requestmapping (value= "a")
Parameters in processing request:
Public String aa (@xxx annotation ("parameter x/y/z)" String xyz) {}
Notes learned so far include:

Tags: Java Spring MVC

Posted by vzwhaley on Mon, 30 May 2022 07:46:15 +0530