欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring mvc代码示例

程序员文章站 2023-12-30 10:04:10
Handler对应的参数代码实敲import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.context.annotation.Scope;import org.sprin...

Handler对应的参数代码实敲

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

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
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 com.m.entity.User;

@Controller
@Scope("prototype")
@RequestMapping("/test2")
public class TestController2 {
	@RequestMapping("/t1")
	public void test1() {
		System.out.println("t1 已抵达");
	}
	@RequestMapping("/t2")
	public void test2(HttpServletRequest req, 
			HttpServletResponse resp,
				HttpSession session,
					ServletContext application
			) {
		System.out.println("t2 已抵达");
	}
	@RequestMapping("/t3")
	public void test3(String arg1 , Integer arg2) {
		System.out.println("t3 已抵达:" + arg1);
		System.out.println("t3 已抵达:" + arg2);
	}
	@RequestMapping("/t4")
	public void test4(User user,String username) {
		System.out.println("t4 已抵达:" + username);
		System.out.println("t4 已抵达:" + user);
	}
	
	
	//提取路径中的变量值
	@RequestMapping("/t5/{arg1}/{arg2}/{arg3}")
	public void test5(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3) {
		System.out.println("t5 已抵达:" + arg1);
		System.out.println("t5 已抵达:" + arg2);
		System.out.println("t5 已抵达:" + arg3);
	}
	
	//限制请求类型
	@RequestMapping(value = "/t6/{arg1}/{arg2}/{arg3}",
			method= {RequestMethod.POST,RequestMethod.GET})
	public void test6(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3) {
		System.out.println("t6 已抵达:" + arg1);
		System.out.println("t6 已抵达:" + arg2);
		System.out.println("t6 已抵达:" + arg3);
	}
	
	
	//限制请求参数
	@RequestMapping(value = "/t7/{arg1}/{arg2}/{arg3}",
			method= {RequestMethod.POST,RequestMethod.GET},
			params= "random=123")
	public void test7(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3) {
		System.out.println("t7 已抵达:" + arg1);
		System.out.println("t7 已抵达:" + arg2);
		System.out.println("t7 已抵达:" + arg3);
	}
	
	
	//ant风格,?号代表后面一个字符
	//id1,ida……允许
	//id11,idaa,id……不允许
	@RequestMapping(value = "/t8/{arg1}/{arg2}/{arg3}/id?")
	public void test8(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3) {
		System.out.println("t8 已抵达:" + arg1);
		System.out.println("t8 已抵达:" + arg2);
		System.out.println("t8 已抵达:" + arg3);
	}
	
	//ant风格,*号代表后面N个字符
	//id11,idaa,id……都允许
	@RequestMapping(value = "/t9/{arg1}/{arg2}/{arg3}/id*/{arg4}")
	public void test9(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3,
			@PathVariable String arg4) {
		System.out.println("t9 已抵达:" + arg1);
		System.out.println("t9 已抵达:" + arg2);
		System.out.println("t9 已抵达:" + arg3);
		System.out.println("t9 已抵达:" + arg4);
	}
	
	//ant风格,**号可以匹配任意长度,可空
	@RequestMapping(value = "/t10/{arg1}/{arg2}/{arg3}/**/{arg4}")
	public void test10(@PathVariable String arg1,
			@PathVariable String arg2,
			@PathVariable String arg3,
			@PathVariable String arg4) {
		System.out.println("t10 已抵达:" + arg1);
		System.out.println("t10 已抵达:" + arg2);
		System.out.println("t10 已抵达:" + arg3);
		System.out.println("t10 已抵达:" + arg4);
	}
	
	//ant风格
	//多路径映射
	@RequestMapping(value = {"test11","test12"})
	public void test11_12() {
		System.out.println("test11_12 已抵达");
	}
	
	//当参数某个值没有或者不匹配,required用来确定是否是必须的,默认是true;defaultValu表示当没有值的时候,给这个默认值
	@RequestMapping(value = "/t11")
	public void test11(@RequestParam(name="str",required=false,defaultValue="abc") String arg1,
			@RequestParam(name="num",required=false,defaultValue="123") int arg2) {
		System.out.println("t11 已抵达:" + arg1);
		System.out.println("t11 已抵达:" + arg2);
	}
	
	
	
	
	
}

Handler视图解析器代码实敲
配置文件配置视图解析器*

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
	<property name="prefix" value="/jsp/"/><!-- 前缀 -->
	<property name="suffix" value=".jsp"/><!-- 后缀 -->
</bean>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Scope("prototype")
@RequestMapping("/test3")
public class TestController3 {
	//传统的jsp解析方式
	@RequestMapping("/t1")
	public void test1(HttpServletRequest req,
				HttpServletResponse resp) {
		try {
			System.out.println("t1 已抵达");
			//转发
			req.setAttribute("message", "this is mvc message1");
			req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	//通过ModelAndView
	@RequestMapping("/t2")
	public ModelAndView test2() {
		System.out.println("t2 已抵达");
		ModelAndView mv = new ModelAndView("/jsp/index.jsp");
		mv.addObject("message", "this is mvc message2");
		return mv;
	}
	
	//通过ModelMap
	@RequestMapping("/t3")
	public String test3(ModelMap map) {
		System.out.println("t3 已抵达");
		map.addAttribute("message", "this is mvc message3");
		return "/jsp/index.jsp";
	}
	
	//通过ModelMap了,通过解析器里面的前后缀,使得代码简洁
	@RequestMapping("/t4")
	public String test4(ModelMap map) {
		System.out.println("t4 已抵达");
		map.addAttribute("message", "this is mvc message4");
		return "index";
	}
	
	@RequestMapping("/t5")
	public String test5(ModelMap map,String arg1) {
		System.out.println("t5 已抵达");
		System.out.println("t5 已抵达:"+arg1);
		map.addAttribute("message", "this is mvc message5");
		return "forward:/jsp/index.jsp";
	}
	
	//通过forward完成内部controller之间的转发,重定向
	@RequestMapping("/t6")
	public String test6(ModelMap map) {
		System.out.println("t6 已抵达");
		return "forward:/test3/t5.do?arg1=abc";
	}
}

@ResponseBody返回json

@Controller
@Scope("prototype")
@RequestMapping("/test5")
public class TestController5 {
	@ResponseBody//把所有的返回类型变为json对象进行返回
	@RequestMapping("/ajax")
	public Object ajax() throws IOException {
		User user = new User();
		user.setId(1);
		user.setUsername("xxxx");
		user.setPassword("123");
		//map array list object string
		return user;
	}
	
}

本文地址:https://blog.csdn.net/weixin_43770224/article/details/110855014

相关标签: java

上一篇:

下一篇: