springmvc-03-跳转和转发和前端交互

1.方式一(视图解析器)

页面: 前缀+viewName+后缀
第一步:视图解析器里配置前缀和后缀

<!--    视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--&lt;!&ndash;        前缀 &ndash;&gt;一般来说,都一样-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--        后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

2.实现类里写viewName

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

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

public class ControllerTest1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","hello");
        mv.setViewName("test");
        return mv;
    }
}

setViewName就是文件名

2.方式二(不用视图解析器)

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/c1")
public class ControllerTest2 {
   // 转发
    @RequestMapping("s1")
    public String test1(Model model){
        model.addAttribute("msg","hello,方式一!");
        return "/index.jsp";
    }
    // 转发
    @RequestMapping("s2")
    public String test2(Model model){
        model.addAttribute("msg","hello,方式二!");
        return "forward:/index.jsp";
    }
    //重定向
    @RequestMapping("s3")
    public String test3(Model model){
        model.addAttribute("msg","hello,方式三!");
        return "redirect:/index.jsp";
    }
}

3.从前端获取参数

三种情况:

  • 1.前端的参数名称和方法里的参数名称一致的情况
  • 2.前端的参数名称和方法里的参数名称不一致的情况
  • 3.返回注入对象

import com.wang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class UserController {
    // 前端的参数名称和方法里的参数名称一致的情况
    @GetMapping("/t1")
    public String test1(String name, Model model){
        //同名情况,接收前端参数
        System.out.println("接收到的前端的参数是:"+name);
        //返回给前端
        model.addAttribute("msg",name);
        return "test";
    }
//    前端的参数名称和方法里的参数名称不一致的情况,建议就算一致也应该把注解加上,安全,规范
//    当方法的参数和前端的参数一样时而注解的参数和前端不一致的时候,仍然会报错,所以注解优先级更高一些
    @GetMapping("t2")
    public String test2(@RequestParam("username") String name,Model model){
        System.out.println("接收到的前端的参数是:"+name);
        model.addAttribute("msg",name);
        return "test";
    }
    //返回参数需要注入进对象的时候,只要参数名对应就可以注入,不一致赋值为空
    @GetMapping("t3")
    public String test3(User user,Model model){
        System.out.println("接收到的前端的参数是:"+user);
        model.addAttribute("msg",user);
        return "test";
    }
}

4.向前端返回参数

1.ModelAndView方式

public class ControllerTest1 implements Controller {

   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //返回一个模型视图对象
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

2.ModelMap

@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
   //封装要显示到视图中的数据
   //相当于req.setAttribute("name",name);
   model.addAttribute("name",name);
   System.out.println(name);
   return "hello";
}

3.Model方式

@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
   //封装要显示到视图中的数据
   //相当于req.setAttribute("name",name);
   model.addAttribute("msg",name);
   System.out.println(name);
   return "test";
}

4.乱码问题

在web.xml中配置过滤器

<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值