Training summary day4

Under today's training content but also quite a bit more complicated, summed up is more difficult, and unfortunately today a cold, nose uncomfortable for a day, the body of the case over false or teacher to keep up with the pace as much as possible, at the moment cough, runny nose plus neck pain, I decided to record all the contents of today's training as detailed as possible, the original purpose of a blog is to record the process of learning it, let the future look up rules to follow and quantify the progress of each process, but also give the future their leave good memories.

SpringMVC fifth data submission:

Fifth: dynamic data placeholder to submit
summary yesterday SpringMVC in four common data submission , the teacher talked about today fifth: a note @PathVariable using the framework provided by the value of the url of the request as a parameter collection limitation is that this way of data submission is limited to a hyperlink.

<a href="${pageContext.request.contextPath}/admin/five/中国/66.action">
第五种:动态占位符提交数据</a>
   	@RequestMapping("/five/{name}/{age}")
    public String five(@PathVariable(value = "name") String name,@PathVariable(value = "age") int age){
        System.out.println(name+"-----"+age);
        return "main";
    }

In this way the data submitted the data to be passed on the url requested action, the difference is that it is the direct use of the slash / separated from the data and requests instead of a question mark, which means that data has become request action an integral part. @PathVariable then used to parse each @RequestMapping ( "/ five / {name} / {age}") placeholder. This process, often used in distributed development, the data submitted in the address bar is not visible.

SpringMVC page jump four major ways:

In fact, the page jump way is often divided into two types, one is forwarding a redirect.
Process the request process is this: the client first sends a request to the server (that is, our Controller) is received, the server according to the request, find processing methods corresponding to the request processing method is completely decided my page eventually request is forwarded to a page, or a request is forwarded to the action, or redirected to a page, or a redirect to action.
Forwarding can be understood as the client's request is fully able to handle my server, server to be forwarded,
the difference between forwarding and redirection of:
1) the browser address access -----> Forward does not change, alter redirect
2) the number of requests sent ----> 1 forward the request, redirect 2
3) whether the portable request () parameter -----> may be carried forward, redirect not bring
4) to access the web-INF resource ---- > forward can access, not access redirection

SpringMVC Upload Files:

1. Import jar package
commons-fileupload-1.2.2.jar-IO-1.3.2.jar and Commons
Here Insert Picture Description
2. Springmvc.xml configuration file in:

 <bean id = "multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  </bean>

3. Submit form form data using the file upload manner, the value to be noted that modify enctype multipart / form-data, which indicates the default mode for transmitting the key to the file streamed

  <form action="${pageContext.request.contextPath}/admin/up.action" 
  method="post" enctype="multipart/form-data">
    <p>图片:</p>
    <input type ="file" name = "myfile">
    <input type ="submit" value="上传">
  </form>

4. Then the write controller. To file upload, the controller class to complete the following three tasks:

  • Build file name, use UUID naming, use the original file suffix
  • Specified path stored at the server
  • Memory for file transfer

Use UUID named, in order to ensure the uniqueness of the name of the file. UUID generated with dashes itself - we need to be removed:. Uuid.toString () replace ( "-", ""), and then, we need to extract the original file name suffix. For example, after the full name of the original file is called dog1.jpg, for the original file name dog1 we do not care, we just want to get his name suffix (that is, his type), with this type, we can use to remove dash of stitching the UUID suffix, get a new file name, so that not only ensures the uniqueness of the file name and file type so that will not change. Here are a tool provided by the class teacher helped us define a good method to achieve the above functions:

package com.sxau.demo.utils;

import java.util.UUID;

public class FileNameUtil {
	//根据UUID生成文件名
	public static String getUUIDFileName() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}
	//从请求头中提取文件名和以及文件的后缀
	public static String getRealFileName(String context) {
		// Content-Disposition: form-data; name="myfile"; filename="a_left.jpg"
		int index = context.lastIndexOf("=");
		String filename = context.substring(index + 2, context.length() - 1);
		return filename;
	}
	//根据给定的文件名截取后缀
	public static String getFileType(String fileName){
		//9527s.jpg
		int index = fileName.lastIndexOf(".");
		return fileName.substring(index);
		//8b7cb153974c4947ad6934a3213dd1f4.jpg
	}
}

Then we can call the tool defined in the class a good way to work up.

Published 46 original articles · won praise 16 · views 2638

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/103818730