SpringMVC入门——day2
一 . 使用注解@Controller
@Controller注解用于 声明Spring类的 实例,是一个控制器
功能和@controller相同的三个注解:
@Component:组件
@Service: service层
@Repository: dao(pojo)层
Spring通过扫描机制来找到 所有基于注解的控制器类,为了保证Spring能找到我们编写的控制器,需要在配置文件中声明组件扫描
<!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 --> <context:component-scan base-package="com.ma.controller"/>
实现类:
@Controller //代表这个类会被 Spring接管, //此类下 所有返回值是String的,并且有具体页面可以跳转的方法 //都会被视图解析器 解析到 public class ControllerTest2 { //映射访问路径 @RequestMapping("/t2") public String test1(Model model){ model.addAttribute("msg","ControllerTest2"); return "test"; } }
二 . 使用注解@RequestMapping
@RequestMapping用于将url映射到一个指定的方法或者类上
用于方法时访问路径为:拼接前缀+h1+拼接后缀
@Controller public class TestController { @RequestMapping("/h1") public String test(){ return "test"; } }
同时用于类和方法时,访问路径为:拼接前缀+类上的路径(/admin)+方法上的路径(/h1)+拼接后缀
@Controller @RequestMapping("/admin") public class TestController { @RequestMapping("/h1") public String test(){ return "test"; } }
三 . 跳转与重定向
1 . ModelAndView对象,根据View的名称,和视图解析器跳转到指定的页面
最原始的方法如下
<!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean>
对应实现类
public class ControllerTest1 implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //返回一个模型视图对象 ModelAndView mv = new ModelAndView(); mv.addObject("msg","Test1Controller"); mv.setViewName("test"); return mv; } }
2 . ServletAPI
通过设置ServletApI的HttpServletResponse实现输出、重定向、转发,不需要视图解析器
@Controller public class ResultGo { @RequestMapping("/result/t1") public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException { rsp.getWriter().println("Hello,Spring BY servlet API"); } @RequestMapping("/result/t2") public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException { //重定向
rsp.sendRedirect("/index.jsp"); } @RequestMapping("/result/t3") public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception { //转发 req.setAttribute("msg","/result/t3"); req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp); } }
3 . SpringMVC
没有视图解析器时:在 retrurn 后面添加forward或 redirect实现转发和重定向,路径和后缀需要写全
@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"; } }
有视图解析器时,转发直接在return后跟需要拼接的文件名,重定向还是加redirect
@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为另一个请求/ } }
四 . 处理提交的数据
1 . 提交的域名称和处理方法中定义的参数名一致时
提交:http://localhost:8080/hello?name=redfish
@RequestMapping("/hello")
public String hello(String name,Model model){
model.addAttribute("msg",name);
return "hello";
}
结果为:redfish
2 . 提交的域名和处理方法中定义的参数名不一致时,需要在参数前添加@RequestParam并写入域名中输入的参数名
提交:http://localhost:8080/hello?username=redfish
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name,Model model){
model.addAttribute("msg",name);
return "hello";
}
结果为:redfish
3 . 提交的是一个对象时,要求与对象的属性名称一一对应
提交数据 : http://localhost:8080/hello?name=redfish&id=6&age=18
@GetMapping("/hello")
public String hello(User user){
System.out.println(user);
return "test";
}
结果为:User(id=6, name=redfish, age=18)
五 . 把数据显示到前端
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";
}
六 . 乱码问题的解决
SpringMVC提供了一个过滤器,可以在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>
浙公网安备 33010602011771号