2021-11(java-springmvc学习笔记二)

目录

一、控制器Controller

二、注解

三、restful风格

四、跳转方式

无视图解析器的重定向和转发

有视图解析器的

五、数据处理

六、数据显示到前端

第一种

第二种:ModelMap

第三种:Model

七、乱码问题


一、控制器Controller

  1. 编写一个类,实现Controller接口
  2. 使用ModelAndView对象
  3. 去spring配置文件中,注册bean
  4. 编写jsp文件

缺点:一个控制器中只能有一个方法

二、注解

  1. 在spring配置文件中声明组件扫描包
    <!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->
    <context:component-scan base-package="com.kuang.controller"/>
  2. 创建类,使用注解@Controller
  3. 在方法上@RequestMapping,编写路径
  4. return 返回视图位置

控制器和视图之间是弱耦合关系

三、restful风格

传统方式:

http://127.0.0.1/item/queryItem.action?id=1  查询get

http://127.0.0.1/item/saveItem.action  新增post

http://127.0.0.1/item/updateItem.action  更新post

http://127.0.0.1/item/deleteItem.action?id=1  删除get

使用restful

http://127.0.0.1/item/1  查询get

http://127.0.0.1/item  新增post

http://127.0.0.1/item  查询put

http://127.0.0.1/item/1  查询delect

测试:

1、新建一个类

@Controller
public class RestFulController {
}

2、使用注解

@Controller
public class RestFulController {

   //映射访问路径
   @RequestMapping("/commit/{p1}/{p2}")
   public String index(@PathVariable int p1, @PathVariable int p2, Model model){
       int result = p1+p2;
       //Spring MVC会自动实例化一个Model对象用于向视图中传值
       model.addAttribute("msg", "结果:"+result);
       //返回视图位置
       return "test";
  }
   
}

路径与方法不匹配则会报错:400

也可以在注解中使用method指定类型

//映射访问路径,必须是POST请求
@RequestMapping(value = "/hello",method = {RequestMethod.POST})
public String index2(Model model){
   model.addAttribute("msg", "hello!");
   return "test";
}

我们浏览器默认get请求。如果为post,则会报错405

结论:

@RequestMapping注解能处理(get,post,put,delete。path)

所有的地址栏请求默认都会是http get类型的

四、跳转方式

页面:视图解析器前缀+name+视图解析器后缀

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     id="internalResourceViewResolver">
   <!-- 前缀 -->
   <property name="prefix" value="/WEB-INF/jsp/" />
   <!-- 后缀 -->
   <property name="suffix" value=".jsp" />
</bean>
        ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;

无视图解析器的重定向和转发

@Controller
public class ResultSpringMVC {
   @RequestMapping("/rsm/t1")
   public String test1(){
       //转发
       return "/index.jsp";
  }

   @RequestMapping("/rsm/t2")
   public String test2(){
       //转发二
       return "forward:/index.jsp";
  }

   @RequestMapping("/rsm/t3")
   public String test3(){
       //重定向
       return "redirect:/index.jsp";
  }
}

有视图解析器的

@Controller
public class ResultSpringMVC2 {
   @RequestMapping("/rsm2/t1")
   public String test1(){
       //转发
       return "test";
  }

   @RequestMapping("/rsm2/t2")
   public String test2(){
       //重定向
       return "redirect:/index.jsp";
       //return "redirect:hello.do"; //hello.do为另一个请求/
  }

}

重定向不需要视图解析器,本身就是重新请求一个地方,所以有视图解析器的时候注意路径拼接

五、数据处理

提交的名称个处理方法一样

@RequestMapping("/hello")
public String hello(String name){
   System.out.println(name);
   return "hello";
}

提交的名称和方法不一样(如提交:username)

//@RequestParam("username") : username提交的域的名称 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
   System.out.println(name);
   return "hello";
}

提交的为一个的对象

实体类

public class User {
   private int id;
   private String name;
   private int age;
   //构造
   //get/set
   //tostring()
}
@RequestMapping("/user")
public String user(User user){
   System.out.println(user);
   return "hello";
}

name=daiyu&id=15&age=18

结论:如果为对象的话,前端传递的参数名必须和对象名一致,否则为null

六、数据显示到前端

第一种

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;
  }
}

第二种: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";
}

第三种: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";
}

七、乱码问题

springmvc提供的过滤器

<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>

但是这个方法对get支持不好

修改tomcat配置文件:

<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
          connectionTimeout="20000"
          redirectPort="8443" />

自定义过滤器:百度

猜你喜欢

转载自blog.csdn.net/qq_45688193/article/details/121383844