SpringMVC (c)

springmvc file upload

1. Import jar package

 

 Download: https://github.com/suyirulan/putao/tree/master/fileupload_jar

 

jsp page:

<%@ 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>
    <form action="addStudent" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="name" /><br>
        年龄:<input type="text" name="age" /><br>
        头像:<input type="file" name="photo" /><br>
        <input type="submit" value="上传" />
    </form>
</body>
</html>

Note: The properties sheet form tag method = "post" must be submitted by post; enctype = "multipart / form-data" is to be written, and the file name = "" also must write

 

2. Upload the configuration file parser springmvc

    <!-- 配置文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="209715200"></property>
    </bean>

Note: The value in units of bytes; bytes: 1M = 1024k = 1024 * 1024 = 1048576 bytes

 

3. Controller layer processing control codes

package com.zhiyou100.xg.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou100.xg.bean.Student;

@Controller
public class StudentController {
    //文件上传
    @RequestMapping("addStudent")
    public String addStudent(Student stu,MultipartFile photo,HttpServletRequest request,Model model) {
        String path = request.getServletContext().getRealPath("/photo");
        File file = new File(path);
        if(!file.exists()) {
            file.mkdirs();
        }
        
        String filename = System.currentTimeMillis()+ photo.getOriginalFilename();
        stu.setPhotos("photo/"+filename);
        
        File targetfile = new File(path+"/"+filename);
        try {
            FileUtils.writeByteArrayToFile(targetfile, photo.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        model.addAttribute("stu", stu);
        return "index";
    }
}

 

 

2.springmvc interceptors

1. Create a class that implements the interface Handlerinterceptor

2. The method of rewriting interface

1 public class MyInterceptor implements HandlerInterceptor{
 2 
 3     @Override
 4     public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
 5             throws Exception {
 6         // TODO Auto-generated method stub
 7         
 8     }
 9 
10     @Override
11     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
12             throws Exception {
13         // TODO Auto-generated method stub
14         
15     }
16 
17     @Override
18     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
19         Object name = request.getSession().getAttribute("name");
20         if(name!=null) {
21             return true;
22         }else {
23             System.out.println("拦截器");
24             return false;
25         }
26     }
27
28 }

 

3. Configure interceptors in springmvc profile

    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <mvc:exclude-mapping path="/user/tologin"/><!-- 不过滤login页面  -->
            <mvc:exclude-mapping path="/user/login"/>
            <mvc:exclude-mapping path="/user/register"/>
            <bean class="com.zhiyou100.xg.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

 

4. Controller layer processing control codes

package com.zhiyou100.xg.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;



@Controller
@RequestMapping("/user")
@SessionAttributes(names= {"name","address"})//键名叫:name保存的作用域为session
public class UsersController {
    
    @RequestMapping("tologin")
    public String tologin(){
        return "login";
    }
    
    
    @RequestMapping("login")
    public String login(String name,String password ,Model model) {
        if("admin".equals(name)&&"admin".equals(password)) {
            model.addAttribute("name", name);
            return "list";
        }else{
            return "login";
        }
        
    }
    
    @RequestMapping("register")
    public String register() {
        return "register";
    }
    
    
    @RequestMapping("List") //If not logged in, then the access request address is returned to the login page 
    public String List () { 
        System.out.println ( "query all user information" );
         return "List" ; 
    } 


}

 

Demand: index.jsp; register.jsp; login.jsp do not need to filter and list pages to see the successful landing

jsp page placement

 

 

 

1.index.jsp page

<%@ 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>
<a href="user/tologin">登录</a>
<a href="user/register">注册</a>
<a href="user/list">查询</a>
<a href="user/delete">删除</a>

</body>
</html>

 

2.login.jsp page

<%@ 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>
    <form action="login" method="post">
        用户名:<input type="text" name="name"/><br>
        密码:<input type="text" name="password"/><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

 

3.register.jsp page

<%@ 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>
    <form action="users/register" method="post">
    姓名:<input type="text" name="unames"/><br>
    密码:<input type="text" name="password"/><br>
    手机:<input type="text" name="phone"/><br>

    <input type="submit" value="提交"/>
    </form>
</body>
</html>

4.list.jsp page

<%@ 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>
==========list============
</body>
</html>

 

3.springmvc background check

1. introduced jar package

 

 

2. Add annotations corresponding entity classes

 

 

 

Detailed notes:

 

 

3. When receiving a control parameter layer

 

 4.jsp page

 

Guess you like

Origin www.cnblogs.com/xg123/p/11462737.html