springmvc----demo1---hello---bai
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; 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.support.SessionStatus; import org.springframework.web.portlet.bind.annotation.RenderMapping; import org.springframework.web.servlet.ModelAndView; import com.etc.entity.Book; import com.etc.entity.Student; //通过注解创建1个控制器对象 @Controller public class HelloAction { public HelloAction() { System.out.println( "构造了!!!" ); } //通过注解配置该方法处理含有hello的请求 @RequestMapping ( "/hello" ) public String hello() { //返回一个视图标识.自动被映射到/WEB-INF/jsp/hello.jsp的文件 return "hello" ; } //通过modelandview返回模型和视图的打包 @RequestMapping ( "/showdata" ) public ModelAndView showdata() { Map<String,Object> data = new HashMap<String,Object>(); data.put( "str" , " i am bai" ); //存储字符串 data.put( "stu" , new Student( 1 , "白" )); //存储字符串 ModelAndView ma = new ModelAndView( "showdata" , data); return ma; } @RequestMapping (method=RequestMethod.GET,value= "/showinput" ) public ModelAndView showinput( @RequestParam (value= "a" ,required= true ) String a, @RequestParam (value= "b" ,required= false ) String b ) { Map<String,Object> data = new HashMap<String,Object>(); data.put( "a" , a); //存储字符串 data.put( "b" , b); //存储字符串 ModelAndView ma = new ModelAndView( "showinput" , data); return ma; } //在请求作用域初始化1个空模型 @ModelAttribute ( "student" ) public Student initStudent() { return new Student(); } @ModelAttribute ( "book" ) public Book initBook() { return new Book(); } //用于输入一个学生。练习,创建Animal类,aid,aname,feetcount。使用模型驱动输入1个动物并显示。 @RequestMapping ( "/input_stu" ) public String showinput_stu ( @ModelAttribute ( "student" ) Student stu , @ModelAttribute ( "book" ) Book book) { return "show_stu" ; } @RequestMapping (value= "/{a}/{b}/input_path" ) public ModelAndView inputpath( @PathVariable String a, @PathVariable String b) { Map<String,Object> data = new HashMap<String,Object>(); data.put( "a" , a); //存储字符串 data.put( "b" , b); //存储字符串 ModelAndView ma = new ModelAndView( "showinput_path" , data); return ma; } //使用rest风格,录入1个学生 @RequestMapping (value= "/{sid}/{sname}/input_stu_path.html" ) public ModelAndView inputpath( @PathVariable int sid, @PathVariable String sname) { Map<String,Student> data = new HashMap<String,Student>(); data.put( "stu" , new Student(sid, sname) ); //存储学生 ModelAndView ma = new ModelAndView( "showinput_stu_path" , data); return ma; } //使用老方法获取request,session @RequestMapping (value= "/getsession1" ) public String getsession1(HttpServletRequest request) { request.setAttribute( "r" , "rrrrrrrrrrr" ); HttpSession s = request.getSession(); s.setAttribute( "s1" , "11111111" ); return "show_session1" ; } //使用新方法获取session @RequestMapping (value= "/getsession2" ) public String getsession1(HttpSession session ,HttpServletRequest request ,SessionStatus sta) { //如果该session被删除,需要重建*/ if (sta.isComplete()) session = request.getSession(); //重建sessin session.setAttribute( "s2" , "222222" ); //session.invalidate(); //删除整个session return "show_session2" ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步