SpringMVC介绍
2021-03-07 02:30
标签:hello tin Servle 资源 编写 ons oid bin std Spring-web SpringMVC springmvc.xml idea,添加lib文件夹 参数列表 springmvc.xml 如果不使用rest,那么增删改查就需要建立四个接口,如果用rest那么用GET,POST,PUT,DELETE四个提交方式对应增删改查 web.xml 需要设置过滤request和response的编码方式,可以自己手动编写过滤器,也可以由现成的框架实现 post:分别设置request和response编码格式 get:在tomcat的server.xml文件中,添加URIEncoding=utf-8 在一个应用程序中,可能包含n多个过滤器,一般没有顺序要求,如果设置了编码过滤器,必须要将编码过滤器设置到最上面,保证编码过滤器,优先执行 编码过滤器,要放在最上面的位置 每次tomcat可以启动,可以自己启动 SessionAttributes要注意,在使用的时候如果没有对应的值 重定向操作也不会经过视图处理器 springmvc.xml 默认的servlet处理类 保证动态请求和静态资源能够访问 springmvc.xml value的值越大,优先级越小 添加自定义的类型转换器到ConversionServiceFactoryBean中 springmvc.xml JSR303提供的标准校验 springmvc_json @RequestBody 表示把当前请求的内容 @ResponseBody 解析字符串为标签 springmvc.xml 执行顺序,preHandler->目标方法->postHandler->页面跳转->afterCompletion 如果执行过程中抛出异常,那么afterCompletion依然会继续执行 执行顺序 login_en_US.properties login_zh_CN.properties jsp文件body内容 springmvc.xml jsp文件的body标签前不要漏下以下配置 容器启动好后,进入DispatcherServlet之后,会对HandlerExceptionResolver进行初始化操作 可以通过@ControllerAdvice注解进行标注,表示为全局异常处理类 如果有了全局异常,若当前类抛出了异常会现在本类查找,然后再查找全局异常 @ResponseStatus,可以标注在方法中,但是不推荐使用 Springmvc流程图 SpringMVC介绍 标签:hello tin Servle 资源 编写 ons oid bin std 原文地址:https://www.cnblogs.com/YC-L/p/14258436.html什么是MVC
最典型的MVC就是JSP+servlet+JavaBean的模式
public class MyServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String username = req.getParameter("username");
req.getSession().setAttribute("username", username);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
this.doPost(req. resp);
}
}
web.xml配置servlet
public class HelloController{
public void hello(){
System.out.println(this.getClass().getName());
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
// 创建对象
ModelAndView mv = new ModelAndView();
// 添加视图名称, 要跳转的页面名称
mv.setViewName("hello");
// 向前端页面添加属性值
mv.addObject("hello", "hello, springmvc");
return null;
}
}
Spring的处理过程
@Controller
public class HelloController{
@RequestMapping("/hello")
public String hello(){
map.put("hello", "hello,SpringMVC");
return "hello";
}
@RequestMapping("/hello")
public String hello2(Map
常用的注解
@Controller
public class PathVariableController{
@RequestMapping("/testPathVariable/{name}")
public String testPathVariable(@PathVariable String name){// rest方式用的比较多
request.getParameter("name");
System.out.println(name);
return "hello";
}
}
@Controller
public class PathVariableController{
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") String name){// rest方式用的比较多
request.getParameter("name");
System.out.println(name);
return "hello";
}
}
@Controller
public class PathVariableController{
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id, @PathVariable("name") String name){// rest方式用的比较多
request.getParameter("name");
System.out.println(name);
return "hello";
}
}
web.xml
public class UserDao{
public void save(User user){
System.out.println("save");
}
public void update(Integer id){
System.out.println("update");
}
public void delete(Integer id){
System.out.println("delete");
}
public void query(){
System.out.println("query");
}
}
public class User{
}
@Controller
public class UserController{
@Autowired
private User userDao;
@RequestMapping("/save")
public String save(){
userDao.save(new User());
return "success";
}
@RequestMapping("/update")
public String update(Integer id){
userDao.update(id);
return "success";
}
@RequestMapping("/delete")
public String delete(Integer id){
userDao.delete(id);
return "success";
}
@RequestMapping("/query")
public String query(){
userDao.query();
return "success";
}
}
REST
导包
public class UserDao{
public void save(User user){
System.out.println("save");
}
public void update(Integer id){
System.out.println("update");
}
public void delete(Integer id){
System.out.println("delete");
}
public void query(){
System.out.println("query");
}
}
public class User{
}
@Controller
public class UserController{
@Autowired
private User userDao;
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String save(){
userDao.save(new User());
return "success";
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public String update(Integer id){
userDao.update(id);
return "success";
}
@RequestMapping(value = "/user", method = RequestMethod.DELETE)
public String delete(Integer id){
userDao.delete(id);
return "success";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String query(){
userDao.query();
return "success";
}
}
public class MyFilter implements Filter{
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){
filterChain.doFilter(servletRequest, servletResponse);
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setCharacterEncoding("UTF-8");
}
} SpringMVC的使用
RequestParam和PathVariable不要混淆
RequestParam的参数
乱码问题解决
调整顺序可以解决乱码问题
@Controller
public class RequestController{
@RequestMapping("/testRequest")
public String testRequest(@RequestParam(value = "username", required = false) String name){
System.out.println(name);
return "success";
}
}
// 获取请求头信息
@Controller
public class RequestController{
@RequestMapping("/testRequest")
public String testRequestHeader(@testRequestHeader String userAgent){
System.out.println(userAgent);
return "success";
}
// request.getCookie()
@RequestMapping("/testCookie")
public String testCookie(@CookieValue("JSESSIONID"), String jsid){
System.out.println(jsid);
return "success";
}
}
@Controller
public class UserController22{
@RequestMapping("/testUser")
public String testUser(User user){
System.out.println(user);
return "success";
}
}
SessionAttribute
@Controller
@SessionAttributes(value = {"username", "msg"}, types = Integer.class) // 每次向request设置作品用于的时候, 顺带向session中保存一份
public class OutputController{
@RequestMapping("/output")
public String output(Map
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用
@Controller
public class UserController{
Object o1 = null;
Object o2 = null;
Model m1 = null;
@RequestMapping("update")
public String update(@ModelAttribute("user2") User user, Model model){
o2 = user;
System.out.prinltn(user);
System.out.println(o1 == o2);// true 指向同一个对象
System.out.println(m1 == model);// false
return "success";
}
@ModelAttribute
public void testModelAttribute(Model model){
User user = new User();
user.setId(1);
user.setName("name");
user.setPassword("1234");
model.addAttribute("user2", user);
o1 = user;
m1 = model1;
}
}
@Controller
public class UserController{
Object o1 = null;
Object o2 = null;
Model m1 = null;
@RequestMapping("update")
public String update(@ModelAttribute("user2") User user, Model model){
o2 = user;
System.out.prinltn(user);
System.out.println(o1 == o2);// true 指向同一个对象
System.out.println(m1 == model);// false
return "success";
}
// 加一个user2标识, 可以匹配到user2
@ModelAttribute("user2")
public void testModelAttribute(Model model){
User user = new User();
user.setId(1);
user.setName("name");
user.setPassword("1234");
model.addAttribute("user2", user);
o1 = user;
m1 = model1;
}
}
forward转发
@Controller
public class ForWardController{
@RequestMapping("/forward")
public String forward(){
System.out.printfln("forward");
return "forward:/index.jsp";
}
@RequestMapping("/forward2")
public String forward2(){
System.out.printfln("forward2");
return "forward:/forward";
}
}
redirct重定向
@Controller
public class Redirctontroller{
@RequestMapping("/redirect")
public String forward(){
System.out.printfln("forward");
return "redirect:/index.jsp";
}
@RequestMapping("/redirect2")
public String forward2(){
System.out.printfln("redirect2");
return "redirect:/redirect";
}
}
转发与重定向的区别
自定义视图解析器
public class MyViewResolver implements ViewResolver, Ordered{
private int order = 0;
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception{
if(viewName.startsWith("test")){
System.out.println(viewName);
return MyView();
}else{
return null;
}
}
}
public void setOrder(int order){
this.order = order;
}
@Override
public int getOrder(){
return 0;
}
public class MyView implements View{
@Override
public String getContentType(){
return null;
}
@Override
public void render(Map
@Component
public class MyConverter implements Converter
自定义日期转换器
// 如果使用默认的类型转换器, 那么在输入日期的时候使用/作为分隔
@Controller
public class DateConverterController{
@RequestMapping("/testDateConverter")
public String testDateConverter(User user){
System.out.println(user);
return "success";
}
}
public class User{
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birth;
}
数据校验
Hibernate Validator 扩展注解
JSR校验框架
导包
日期格式化器
public class Person{
@NouNull
private Integer id;
@Length(min = 3, max = 10)
private String name;
private Integer age;
private String gender;
@Past // 设置的日期只能是之前的日期
private Date birth;
@Email
private String gmail;
}
@Controller
public class ValidationController{
@RequestMapping("/validation")
public String validate(@Valid Person person, BindingResult){
if(BindingResult.hasErrors()){
System.out.println("验证失败");
// 获取到当前所有的错误
List
@Controller
public class JsonController{
@ResponseBody
@RequestMapping("/json")
public String json(){
List
test
";
}
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println(body);
return "success";
}
@RequestMapping("/testRequestJson")
public String testRequestJson(@RequestBody User user){
System.out.println(user);
return "success";
}
}
public class User{
private Integer id;
private String name;
private Integer age;
private String gender;
public User(){
}
public User(String name, Integer age, String gender){
}
} 自定义响应ResponseEntity
@Controller
public class Entity{
@RequestMapping("test")
public String test(HttpEntity
gogogo
";
MultiValueMap servlet下载文件
@Controller
public class DownController{
@RequestMapping("/download")
public String download(HttpServletRequest request){
// 获取下载路径
ServletContext servletContext = request.getServletContext();
servletContext.getRealPath("/scripts/...");
// 通过io流对文件读写
FileInputStream FileInputStream = new FileInputStream(realPath);
byte[] bytes = new byte[FileInputStream.available()];
FileInputStream.read(bytes);
FileInputStream.close();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Disposition", "attachment:filename=...");
return new ResponseEntitybyte[]>(bytes, httpHeaders, HttpStatus.OK);
}
}
servlet上传文件
springmvc.xml
添加jar包
多文件上传
@Controller
public class UploadController{
@RequestMappin("upload")
public String upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam(value = "desc", required = false) String desc){
System.out.println(desc);
for(MultipartFile file: multipartFile){
if(!file.isEmpty()){
System.out.println(file.getOriginalFilename());
multipartFile.transferTo(new File("d:\\file\\" + multipartFile.getOriginalFilename()));
}
}
return "success";
}
}
拦截器和过滤器的区别
public class MyInterceptor implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest request, HttpServletResponse response){
System.out.println(this.getClass().getName());
return true;// 返回指控制流程是否停止
}
public boolean postHandle(HttpServletRequest request, HttpServletResponse response){
System.out.println(this.getClass().getName());
return true;
}
public boolean afterCompletion(HttpServletRequest request, HttpServletResponse response){
System.out.println(this.getClass().getName());
return true;
}
}
@Controller
public class InterceptorController{
@RequestMapping("/testInterceptor")
public String testInterceptor(){
// 如果此处抛出异常, after依然会执行
System.out.println(this.getClass().getName());
return "success";
}
}
国际化
@Controller
public class I18nController{
@Autowired
private MessageSource messageSource
@RequestMapping("/i18n")
public String i18n(Locale locale){
System.out.println(locale);
String username = MessageSource.getMesssage("username", null, locale);
System.out.println(username);
return "login";
}
@RequestMapping("/i18n")
public String i18n(@RequestParam("locale") String localStr, Locale locale, HttpSession session){
Locale locale1 = null;
System.out.println(locale);
if(localStr != null && !"".equals(localStr)){
locale1 = new Locale(localStr.split("_")[0], localStr.split("_")[1]);
}else{
locale1 = locale;
}
session.setAttribute(SessionLocaleResolver.class.getName() + ".LOCALE", locale1);
return "login";
}
}
public class MyLocaleResolver implements LocaleResolver{
public Locale resolveLocale(HttpServletRequest request){
Locale locale = null;
String localeStr = request.getParameter("locale");
if(localeStr != null && !"".equals(localStr)){
locale = new Locale(localeStr.split("_")[0].localeStr.split("_"))
}else{
locale = request.getLocale();
}
return locale;
}
}
mvc异常处理机制
@Controller
public class ExceptionController{
@RequestMapping("/exception1")
public String exception(){
System.out.println(this.getClass().getName());
int i = 10/0;
return "success";
}
@ExceptionHandler(value = ({ArithmeticExcepton.class, NullPointerException.class}))
public String handlerException(Exception exception){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exce", exception);
return mv;
}
@ExceptionHandler(value = (Exception.class))
public String handlerException2(Exception exception){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exce", exception);
return mv;
}
}
@ControllerAdvice
public class MyGlobalExceptionHandler{
@ExceptionHandler(value = ({ArithmeticExcepton.class, NullPointerException.class}))
public String handlerException(Exception exception){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exce", exception);
return mv;
}
@ExceptionHandler(value = (Exception.class))
public String handlerException2(Exception exception){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exce", exception);
return mv;
}
}
ResponseStatus
@Controller
public class ExceptionController{
@ResponseStatus(reason = "", value = HttpStatus.NOT_ACCEPTABLE)
@RequestMapping("/exception")
public String handlerException(Exception exception){
System.out.println("exception")
return "success";
}
}
@Controller
public class ExceptionController{
@RequestMapping("/exception")
public String handlerException(String username){
System.out.println("exception")
if("admin".equals(username)){
return "success";
}else{
throw new UsernameException();
}
return "success";
}
}
上一篇:算法导论随笔(二)
下一篇:C语言-QQ消息轰炸器