您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-03-RequestMapping常用属性

星夜孤帆 发布时间:2019-04-20 19:42:34 ,浏览量:1

  pom.xml

  4.0.0
  com.monkey1024
  02mvc
  0.0.1-SNAPSHOT
  war
  
        
            junit
            junit
            3.8.1
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        

    
    
    	
        02mvc
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                    1.8
                    1.8
                    UTF-8
                
            
        
    
web.xml

  4.0.0
  com.monkey1024
  02mvc
  0.0.1-SNAPSHOT
  war
  
        
            junit
            junit
            3.8.1
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        

    
    
    	
        02mvc
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                    1.8
                    1.8
                    UTF-8
                
            
        
    
springmvc.xml


        
	
	
	
	
	
	
	
	
    
	
	
	
	
	
	
		
		
	
	
	

 
Controller
package com.monkey1024.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


/*
 * 使用注解后就不需要实现接口了
 */
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC  {
	
	
	// 需要处理的url
//	@RequestMapping("/test/test2.do")
	// 只允许提交Post请求
	// 方法名可以随意命名
	@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
	public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持post请求");
		// 连接到jsp
		mv.setViewName("post");
		return mv;
		
		
		
	}
	/*
	 * 以数组的形式,下面两个都可以调用这个方法
	 */
//	@RequestMapping({"/test/hello.do","/test/world.do"})
//	@RequestMapping({"/hello.do","/world.do"})
//	@RequestMapping(value="/test.do",params={"name","age"})
	@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
	public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持get请求");
		// 跳转到jsp
		mv.setViewName("get");
		return mv;
		
		
		
	}

}
get.jsp
package com.monkey1024.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


/*
 * 使用注解后就不需要实现接口了
 */
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC  {
	
	
	// 需要处理的url
//	@RequestMapping("/test/test2.do")
	// 只允许提交Post请求
	// 方法名可以随意命名
	@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
	public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持post请求");
		// 连接到jsp
		mv.setViewName("post");
		return mv;
		
		
		
	}
	/*
	 * 以数组的形式,下面两个都可以调用这个方法
	 */
//	@RequestMapping({"/test/hello.do","/test/world.do"})
//	@RequestMapping({"/hello.do","/world.do"})
//	@RequestMapping(value="/test.do",params={"name","age"})
	@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
	public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持get请求");
		// 跳转到jsp
		mv.setViewName("get");
		return mv;
		
		
		
	}

}
post.jsp 
package com.monkey1024.controller;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


/*
 * 使用注解后就不需要实现接口了
 */
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC  {
	
	
	// 需要处理的url
//	@RequestMapping("/test/test2.do")
	// 只允许提交Post请求
	// 方法名可以随意命名
	@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
	public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持post请求");
		// 连接到jsp
		mv.setViewName("post");
		return mv;
		
		
		
	}
	/*
	 * 以数组的形式,下面两个都可以调用这个方法
	 */
//	@RequestMapping({"/test/hello.do","/test/world.do"})
//	@RequestMapping({"/hello.do","/world.do"})
//	@RequestMapping(value="/test.do",params={"name","age"})
	@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
	public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("method","只支持get请求");
		// 跳转到jsp
		mv.setViewName("get");
		return mv;
		
		
		
	}

}
index.jsp



Hello World

	
	
	
	



	


 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.0380s