当前位置 : 主页 > 编程语言 > c++ >

Spring contruller语法

来源:互联网 收集:自由互联 发布时间:2021-07-03
Spring contruller语法 package orc.dhw.controller;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.
Spring contruller语法
package orc.dhw.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import orc.dhw.bean.Roles;
import orc.dhw.bean.UserInfo;

@Controller
@RequestMapping("/user")
public class UserInfoController {
	
	@ModelAttribute("info")
	public UserInfo initInfo(){
		return new UserInfo(1001,"曹操","123456",2,"男");
	}
	
	
	
	@ModelAttribute("list")
	public List
 
   initList(){
		List
  
    list = new ArrayList
   
    (); list.add(new Roles(1,"boss")); list.add(new Roles(2,"ceo")); list.add(new Roles(3,"kfo")); list.add(new Roles(4,"cfo")); return list; } @RequestMapping(value="/index",method=RequestMethod.GET) public String getInfo(){ System.out.println("get请求 -。-"); return "index"; } @RequestMapping(value="/index",method=RequestMethod.POST) public String getInfo(@ModelAttribute("info")UserInfo info,@RequestParam("file")MultipartFile file,HttpServletRequest request){ String img = ""+System.currentTimeMillis(); String servlet = request.getServletContext().getRealPath("img"); try { file.transferTo(new File(servlet+"//"+img+".jpg")); } catch (Exception e) { e.printStackTrace(); } return "down"; } @RequestMapping(value="/down/{name}") public void down(@PathVariable("name")String name,HttpServletRequest request,HttpServletResponse response){ String servlet = request.getServletContext().getRealPath("img"); File file = new File(servlet+"//"+name+".jpg"); if(file.exists()){ response.addHeader("Content-Disposition","attachment;filename="+name+".jpg"); try { OutputStream output = response.getOutputStream(); byte[] b = new byte[1024]; FileInputStream input = new FileInputStream(file); int i; while((i=input.read(b))>0){ output.write(b); } } catch (Exception e) { e.printStackTrace(); } } } }
   
  
 
网友评论