SpringMVC注解详情

本文包含springmvc注解的所有方式,包括存值,取值,返回json,以及.xml配置

springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<mvc:annotation-driven />

	<context:component-scan base-package="org.lq"></context:component-scan>

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">

		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

web.xml配置过滤器和servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>springMVC_02</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>


	<filter>
		<filter-name>encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	
	
	
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


</web-app>

获取页面传到后台的值–所有方式

package org.lq.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.lq.entity.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("user")
public class UserController {

	@RequestMapping(value = "hello",method = RequestMethod.GET)
	public String hello() {
		System.out.println("进控制器hello");
		return "main";
	}
	
	

	@RequestMapping(value = "hello",method = RequestMethod.POST)
	public String hello1() {
		System.out.println("进控制器hello  post");
		return "main";
	};
	
	@RequestMapping("reg")
	public String reg(HttpServletRequest request) {
		System.out.println(request.getParameter("id"));
		System.out.println(request.getParameter("name"));
		return "main";
		
	}
	
	
	@RequestMapping("reg1")
	public String reg1(Integer id,String name) {
		System.out.println(id);
		System.out.println(name);
		return "main";
		
	}
	
	/**
	 * 只要使用了@requestParam默认required就为true,必须要给值,不然报错,
	 * 可以使用required=false设置不是必须赋值
	 * @param id
	 * @param name
	 * @return
	 */
	@RequestMapping("reg2")
	public String reg2(@RequestParam(value = "a",required = true,defaultValue = "0")Integer id,@RequestParam(value = "b",required=false)String name) {
		System.out.println(id);
		System.out.println(name);
		return "main";
		
	}
	
	
	@RequestMapping("reg3")
	public String reg3(Date birthday,String name) {
		System.out.println(birthday);
		System.out.println(name);
		return "main";
	}
	
	@RequestMapping("reg4")
	public String reg4(String[] fruits ) {
		System.out.println(Arrays.toString(fruits));
		
		return "main";
	}
	
	@RequestMapping("reg5")
	public String reg5(User user ) {
		System.out.println(user);
		
		return "main";
	}
	
	/**
	 * http请求头的信息
	 * @param lanuage
	 * @return
	 */
	@RequestMapping("reg6")
	public String reg6(@RequestHeader("Accept-Language")String lanuage) {
		System.out.println(lanuage);
		return "main";
	}
	
	
	@RequestMapping("reg7")
	public String reg7(@CookieValue("JSESSIONID")String sessionid) {
		System.out.println(sessionid);
		
		return "main";
	}
	
	
	/**
	 * 时间属性编辑器,前端传出的字符串需要转换成功java.util,Date类型的时候会默认使用
	 * 下面的时间转换器转换
	 * @param binder
	 */
	@InitBinder
	public void InitBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sf, true));
	}
	
	
	
	
}

后台存值的所有方式

package org.lq.controller;

import java.util.Map;

import org.lq.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
//value会将key为user和msg2的model存到session中
@SessionAttributes(value = {"user","msg2"})
//types会将类型为User的类型的所有model存到session中
//@SessionAttributes(types = {User.class})
public class ResponseSpring {

	@RequestMapping("getString")
	public ModelAndView info() {
		ModelAndView mv = new ModelAndView("main");
		mv.addObject("msg","ModelAndView的值");
		
		return mv;
	}
	
	@RequestMapping("getString2")
	public String info(Map<String,Object> map) {
		map.put("msg2", "自定义map的值***");
		return "main";
	}
	
	@RequestMapping("getString3")
	public String info(ModelMap map) {

		map.addAttribute("msg3", "ModelMap的值");
		return "main";
	}
	
	@RequestMapping("getString4")
	public String info(Model map) {
		
		map.addAttribute("msg4","model接口的值");
//		如果指定跳转方式将不再走视图解析器,必须将跳转地址全部填写完整
		return "forward:main.jsp";
	}
	
	@RequestMapping("getString5")
	public String info2(Model map) {
		
		map.addAttribute("msg5","重定向的值");
//		如果时重定向跳转,将直接把传递的内容拼接到跳转地址的后面(一般类型会,存对象不会拼到后面)
		return "redirect:main.jsp";
	}
	
	@RequestMapping("getobj")
	public String info3(Model map) {
		
		User user = new User();
		user.setName("hahha ");
		map.addAttribute("user",user);
		return "redirect:main.jsp";
	}
}

返回json字符串所有方式

package org.lq.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lq.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ResponseBody {

	@RequestMapping(value = "getstr",produces = "application/json; charset=utf-8")
	@org.springframework.web.bind.annotation.ResponseBody
	public String getstr() {
		return "你好";
	}
	
	@RequestMapping("json1")
	@org.springframework.web.bind.annotation.ResponseBody
	public User getJson() {
		User u = new User();
		u.setName("哈哈哈");
		return u;
		
	}
	
	@RequestMapping("json2")
	@org.springframework.web.bind.annotation.ResponseBody
	public List<String> getJson2() {
		return Arrays.asList("aa","bb","ccc");
		
	}
	
	@RequestMapping("json3")
	@org.springframework.web.bind.annotation.ResponseBody
	public Map<String,String> getJson3() {
		Map<String,String> map = new HashMap<>();
		map.put("a", "aaa");
		map.put("b", "bbb");
		return map;
		
	}
	
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值