springmvc⼊门参数注解
创建servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--核⼼控制器DispatcherServlet,核⼼控制器是⼀个servlet配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Servlet的初始化参数,读取springmvc的配置⽂件,创建spring容器 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</init-param>
<!--配置启⽤时加载对象,Tomcat启动时创建dispatcherservlet类对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--servlet / 固定, /* 是过滤器的-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>
el表达式获取session中的值<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-<!--主要有3点配置-->
<!--包扫描-->
<context:component-scan base-package="com.zl"></context:component-scan>
<!--springmvc:注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--使⽤注解驱动的转换服务-->
<!--<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>-->
<!--内部资源视图解析,对象-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--转换服务交给⼀个对象来做ConversionServiceFactoryBean-->
<!--<bean class="t.support.ConversionServiceFactoryBean" id="conversionService">
<property name="converters">
<bean class="com.zl.utils.MyConverter"></bean>
</property>
</bean>-->
</beans>
控制器
package ller;
import com.zl.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import java.util.List;
import java.util.Map;
@Controller
@SessionAttributes(value = "msg",types = String.class) //将数据存到session域⾥
//@RequestMapping("/hello")
public class HelloController {
/**
* @RequestMapping() 主要作⽤标识请求路径与⽅法的映射关系
* 该注解可以作⽤在⽅法和类上。
* 作⽤在类上:第⼀级的访问⽬录
* 作⽤在⽅法上:第⼆级的访问⽬录
*
* 如果只有⼀个控制器且⼀个处理器那么路径不写的话,相当于/* ,
* 或者说只有⼀个处理器没写路径,那么该处理器就可接受其他所有的路径
* 接收请求
* @return
*/
//@RequestMapping("/hello") //标识请求路径与⽅法的映射关系
//@RequestMapping(value = "/hello",method = RequestMethod.GET) //method不写是各种⽅法都⾏
//@GetMapping("/hello") //GetMapping相当于RequestMapping+get。post等其他⽅法也⼀样。但是他们就不能够作⽤于类上了
//@RequestMapping("/say") //在类上加上@RequestMapping("/hello"),此时⽅法的访问路径就是 localhost:8080/hello/say
//@RequestMapping(value = "/hello",params = {"name=zs","age","address!=jp"}) //参数限制
//@RequestMapping(value = "/hello",headers = {"Accept","!Date","Accept-Encoding!=zh-CN","Host=l
ocalhost:18081"})//对请求头限制,挂了
/**请求参数的绑定
* 绑定机制
* 表单提交的数据都是k=v格式的 username=haha&password=123,所有要⼀⼀对应区分⼤⼩写
* SpringMVC的参数绑定过程是把表单提交的请求参数,作为控制器中⽅法的参数进⾏绑定的
* 要求:提交表单的name和参数的名称是相同的
*/
/*@RequestMapping("/hello")
public String sayHello(String name,int age) { //获取参数,不⽤全部获取,只获取参数名⼀样的,不考虑顺序,⾃动寻同名的参数
System.out.println("Hello SpringMVC!!");
System.out.println(name);
System.out.println(age);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello(User user) { //获取参数直接封装到⼀个对象中,获取属性名与携带参数名⼀样的
System.out.println("Hello SpringMVC!!");
System.out.println(user);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello(@RequestParam Map<String,Object> map) { //要封装到map,list当中要加注解说明请求参数
System.out.println("Hello SpringMVC!!");
System.out.println(map);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello(@RequestParam List<Integer> id) { //要封装到map,list当中要加注解说明请求参数
System.out.println("Hello SpringMVC!!");
System.out.println(id);
return "success";
}*/
/**
* 实体引⽤类型,user⾥封装了course,点出来Course.curname。name=zs&age=19&Course.curname=java&Course.score=99
* user⾥封装了List<Course>,点出来Courses[0].score=99
*/
/*@RequestMapping("/hello")
public String sayHello (User user) {
System.out.println(user);
return "success";
}*/
//post请求中⽂乱码,需在配置⽂件中创建⼀个字符编码过滤器
/*@RequestMapping("/hello")
public String sayHello(String name,int age) {
System.out.println("Hello SpringMVC!!");
System.out.println(name);
System.out.println(age);
return "success";
}*/
/**
*⽇期格式处理
* 1 使⽤注解驱动的转换服务
* 2 推荐⽤spring的⽇期时间注解,@DateTimeFormat
*
*/
/*@RequestMapping("/hello")
public String sayHello (User user) {
System.out.println(user);
return "success";
}*/
//常⽤注解
/*@RequestMapping("/hello")
public String sayHello (@RequestParam(value = "name2",required = false) String name) { //如果写了注解@RequestParam,required不写默认true
System.out.println(name);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello (@RequestBody(required = false) String body) { //@RequestBody接受参数为整个请求体,注意get请求没有请求体
System.out.println(body);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello (@RequestHeader("Accept") String head) { //@RequestHeader请求头,accept不区分⼤⼩写
System.out.println(head);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello (@CookieValue("JSESSIONID") String cookie) {
System.out.println(cookie);
return "success";
}*/
/**
* 作⽤:拥有绑定url中的占位符的。例如:url中有/delete/{id},{id}就是占位符
* 属性
* value:指定url中的占位符名称
* 作⽤:通过占位符来获取路径结果
*/
/*@RequestMapping("/hello/{id}/{id2}") //{}占位符
public String sayHello (@PathVariable String id,@PathVariable String id2) { //作⽤:通过占位符来获取路径结果
System.out.println(id);
System.out.println(id2);
return "success";
}*/
/*@ModelAttribute //相当于初始化
public User parameterUser(){ //有返回值
User user = new User();
user.setAge(18);
return user;
}
@ModelAttribute
public void parameterUser(Map<String,User> userMap){ //⽆返回值,他会把有返回值的覆盖
User user = new User();
user.setAge(23);
userMap.put("user",user);
}
@RequestMapping("/hello")
public String sayHello (User user) {
System.out.println(user);
return "success";
}*/
/*@RequestMapping("/hello")
public String sayHello(Model model) {
//模型数据默认存到request域对象⾥,转发到jsp,通过el表达式来取,${}。要存到其他域要加注解,如@SessionAttributes,不能⽤在⽅法上,⽤类上 model.addAttribute("msg", "你好帅");
return "success";
}*/
@RequestMapping("/add")
public String add(Model model) {
model.addAttribute("msg", "你好帅");
return "success";
}
@RequestMapping("/get")
public String get(ModelMap modelMap) { //存到map容器⾥,所以⽤map来取
System.out.("msg"));
return "success";
}
@RequestMapping("/delete")
public String delete(SessionStatus sessionStatus) {
//设置状态完成,然后会删除域中数据
sessionStatus.setComplete();
return "success";
}
}
⼩发现:如果只有⼀个控制器且⼀个处理器那么路径不写的话,相当于/* ,或者说只有⼀个处理器没写路径,那么该处理器就可接受其他所有的路径bean类
package com.zl.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
private String name;
private int age;
//⽇期格式处理。推荐使⽤spring注解
//@DateTimeFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) //使⽤枚举
private Date birthday;
//set,get,tostring⽅法
}
public class Course implements Serializable {
private String curname;
private int score;
//set,get,tostring⽅法
}
package com.zl.utils;
import verter.Converter;
ParseException;
SimpleDateFormat;
import java.util.Date;
//拓展功能实现转换接⼝
public class MyConverter implements Converter<String,Date> {
@Override
public Date convert(String source) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论