当前位置: 代码迷 >> VC/MFC >> SpringMVC 浜?controller
  详细解决方案

SpringMVC 浜?controller

热度:556   发布时间:2016-05-02 03:12:27.0
SpringMVC 浜?:controller

鐜??鎼?缓濂藉悗灏辨槸Controller鐨勭紪鍐欎簡

1 鍙?互閫氳繃缁ф壙AbstractController锛屽疄鐜皃rotected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp)鏂规硶锛岃?闂?繖涓狢ontroller锛岄粯璁よ?闂?繖涓?柟娉曘?愭?鏂规硶灏辩浉褰揝trust2鐨別xecute()鏂规硶銆?/p>

鍦╗servlet-name]-servlet.xml涓?敞鍐岃繖涓猙ean锛?lt;bean name="xxx.xxx" class = "杩欎釜绫荤殑鍏ㄩ檺瀹氬悕" />

閫氳繃url xxx.xxx璁块棶杩欎釜controller

2 @Controller娉ㄨВ锛屽疄闄呭紑鍙戜腑鍩烘湰涓婁篃閮芥槸杩欑?鏂瑰紡

聽2.1 鍩烘湰褰㈠紡

@RequestMapping(value="/findUser",method=RequestMethod.GET)public String findUser(@RequestParam String username){	System.out.println("username is "+username);	return "example";	//瑙嗗浘鍚嶇О}

[email protected]?鍚庨潰蹇呴』甯?sername锛屽惁鍒欎細鎶ラ敊404锛屽?鏋滀笉鍔犺繖涓?敞瑙d篃鍙?互璧峰埌鍚屾牱 聽 聽鐨勪綔鐢?瀹归敊鎬ц?楂樸??/p>

聽return 杩斿洖鐨勫氨鏄??鍥惧悕绉?锛屼細鏍规嵁[dispatcherservlet-name]-servlet.xml涓璻esolver璺宠浆鍒板?搴旂殑瑙嗗浘銆?/p>

聽2.2 杩斿洖ModelAndView

@RequestMapping(value="/findUser",method = RequestMethod.POST)public ModelAndView findUser(User user){	ModelAndView mv = new ModelAndView("user");	//杩斿洖ModelAndView褰㈠紡,璇曞浘鍚嶇О鍦ㄨ繖閲屽啓	mv.addObject("user", user); //閫氳繃"user"鍙栧??	mv.addObject(user);//閫氳繃user绫诲瀷鍙栧??	return mv;	//杩斿洖ModelAndView}

聽 鍦╱ser.jsp涓?彲浠ュ彇鍒皍ser瀵硅薄

聽 2.3 鍏充簬Bean涓?湁Date绫诲瀷鐨勫睘鎬? [email protected]

@RequestMapping("/todate")public ModelAndView toDate(Date date){     ModelAndView mv = new ModelAndView("user");     mv.addObject("today", "2016-04-19");     mv.addObject(date);     return mv;}//鐢变簬璇锋眰杩囨潵鐨剈rl涓?殑鍙傛暟閮芥槸String绫诲瀷锛屾墍浠ュ綋Bean涓?湁Date鐨勬椂鍊欎細鑷?姩灏哠tring杞?崲涓篋ate@InitBinderpublic void initBinder(ServletRequestDataBinder binder){     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));}

聽 聽3 controller杩斿洖鏁版嵁缁橨SP

聽 聽3.1聽閫氳繃Map鍚戦〉闈?紶閫掑弬鏁?/p>

聽 聽 聽 聽聽

@RequestMapping(value="/show",method=RequestMethod.GET)public String showUser(Map<String,Object> map){	User user = new User("tomcat","tomcat",11);	map.put("user", user);	return "show";}	@RequestMapping("/getuser")public void getPerson(String username,PrintWriter pw){	pw.write("hello : "+username);}

聽 聽 鍜屾櫘閫氭柟寮忎竴鏍? 绗?簩涓?柟娉曞拰Map鏂瑰紡杩斿洖鏁版嵁鏃犲叧锛岄『鎵嬭创涓婂幓

聽 聽 3.2 file upload锛?/p>

聽 聽 jsp椤甸潰锛?/p>

<form action="user/upload" method="post" enctype="multipart/form-data">    <input type="file" name="file"><br>  type鍜宯ame閮藉繀椤绘槸file    <input type="submit" value="submit"></form>

聽 聽controller涓?細

@RequestMapping("/upload")public ModelAndView uploadFile(HttpServletRequest req,ModelAndView mv) throws Exception{	logger.info(">>>>>>>>>涓婁紶鏂囦欢寮?濮?........");	MultipartHttpServletRequest mulReq = (MultipartHttpServletRequest)req;	MultipartFile file = mulReq.getFile("file");//jsp涓璶ame="file"	String originalFilename = file.getOriginalFilename();	String newFileName = originalFilename.substring(0,originalFilename.lastIndexOf('.'));	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");	FileOutputStream fos = new FileOutputStream("D:/app/"+newFileName+"_"+sdf.format(new Date())); //鏂囦欢瀛樺偍浣嶇疆	fos.write(file.getBytes());	fos.flush();	fos.close();	logger.error(">>>>>>>>>涓婁紶鏂囦欢鍑洪敊.......");	return mv;}

聽 聽4 閫氳繃Model 灏嗘暟鎹?繑鍥炵粰jsp

@RequestMapping(value="/list",method=RequestMethod.GET)public String listUser(Model model){	logger.info(">>>>>>>>>杩涘叆userList鏂规硶......");	model.addAttribute("users", users);		return "user/list";}

jsp涓?/p>

<c:forEach items="${users }" var="user">     <a href="${pageContext.request.contextPath }/user/${user.value.username }">${user.value.username }</a>---${user.value.pwd }---${user.value.age }<br></c:forEach>

5 springmvc鐨剅estful褰㈠紡 @PathVariable娉ㄨВ

聽 聽

@RequestMapping(value="/{username}",method=RequestMethod.GET)public String showUser(@PathVariable String username,Model model){	logger.info(">>>>>>>>>杩涘叆showUser鏂规硶......");	model.addAttribute("user", users.get(username));	logger.error(">>>>>>>>>鏌ヨ?鐢ㄦ埛澶辫触.........");	return "user/show";}

瑕佸湪controller涓?殑鑾峰彇request銆乺esponse銆乻esison 鍙?渶瑕佷互褰㈠弬鐨勫疄琛屽紩鍏ュ嵆鍙?/p>

鏂囦腑鐨勬棩蹇楀彲浠ヤ笉绠★紝鏄?殢鎵嬪姞涓婂幓鐨?娌℃湁瀹為檯鎰忎箟

  相关解决方案