zoukankan      html  css  js  c++  java
  • springmvc----demo1---hello---bai

    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";
    	}
    }
    

      

  • 相关阅读:
    Codeforces 845E Fire in the City 线段树
    Codeforces 542D Superhero's Job dp (看题解)
    Codeforces 797F Mice and Holes dp
    Codeforces 408D Parcels dp (看题解)
    Codeforces 464D World of Darkraft
    Codeforces 215E Periodical Numbers 容斥原理
    Codeforces 285E Positions in Permutations dp + 容斥原理
    Codeforces 875E Delivery Club dp
    Codeforces 888F Connecting Vertices 区间dp (看题解)
    Codeforces 946F Fibonacci String Subsequences dp (看题解)
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6270064.html
Copyright © 2011-2022 走看看