欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringMVC框架实现Handler处理器的三种写法

时间:2021-01-09 11:14:53|栏目:JAVA代码|点击:

一、SpringMVC中的处理器

配置完SpringMVC的处理器映射器,处理适配器,视图解析器后,需要手动写处理器。关于处理器的写法有三种,无论怎么写,执行流程都是①处理映射器通过@Controller注解找到处理器,继而②通过@RequestMapping注解找到用户输入的url。下面分别介绍这三种方式。

package com.gql.springmvc;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 类说明:
 * 处理器的三种写法
 * @guoqianliang1998.
 */
@Controller
public class UserController {
 //1.SpringMVC开发方式
 @RequestMapping("/hello")
 public ModelAndView hello(){
 ModelAndView mv = new ModelAndView();
 mv.addObject("msg","hello world!");
 mv.setViewName("index.jsp");
 return mv;
 }
 
 //2.原生Servlet开发方式
 @RequestMapping("xx")
 public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
 request.setAttribute("msg", "周冬雨");
 request.getRequestDispatcher("/index.jsp").forward(request, response);
 }
 
 //3.开发中常用
 @RequestMapping("yy")
 public String yy(Model model){
 model.addAttribute("msg", "双笙");
 return "forward:/index.jsp";//forward写不写都是转发,redirect代表重定向.
 }
}

1.SpringMVC开发方式

 @RequestMapping("/hello")
 public ModelAndView hello(){
 ModelAndView mv = new ModelAndView();
 mv.addObject("msg","hello world!");
 mv.setViewName("index.jsp");
 return mv;
 }

2.Servlet原生开发方式

 @RequestMapping("xx")
 public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
 request.setAttribute("msg", "周冬雨");
 request.getRequestDispatcher("/index.jsp").forward(request, response);
 }
 

3.开发中常用的方式

在return的字符串中,forward写不写都是代表转发,redirect则代表重定向。

 @RequestMapping("yy")
 public String yy(Model model){
 model.addAttribute("msg", "双笙");
 return "forward:/index.jsp";
 }

上一篇:从java中调用matlab详细介绍

栏    目:JAVA代码

下一篇:java设计模式之装饰器模式(Decorator)

本文标题:SpringMVC框架实现Handler处理器的三种写法

本文地址:http://www.codeinn.net/misctech/42635.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有