1.Controller注解配置总结
@Controller放在类上,代表着各类会被spring接管,被这个注解的类中的所有方法,如果返回值是string类型,并且有具体页面跳转,那么就会被视图解析器解析。
@Controller //代表着各类会被spring接管,被这个注解的类中的所有方法,
//如果返回值是string类型,并且有具体页面跳转,那么就会被视图解析器解析。
public class TestController {
@RequestMapping("/t1")
public String test(){
return "test";
}
}
只要改了java的代码就要reload(公开)一下 ,只要改了配置文件就要重新发布tomcat,如果只改了前端页面刷新一下就可以。
控制器和视图之间是弱耦合关系,视图是可以被不同的控制器重复调用的。也就是不同的controller方法返回同一个jsp。
2.@RequestMapping注解
@RequestMapping注解用于映射url到控制器类或者一个特定的处理程序方法,可用于类或方法上。用于类上,作为类中所有所有请求方法的父路径。
组合注解:@GetMapping(@RequestMapping(method = RequestMethod.GET))、
@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping
只注解在方法上面,访问路径:http://localhost:8080/工程名/t1
@Controller
public class TestController {
@RequestMapping("/t1")
public String test(){
return "test";
}
}
同时注解类和方法,访问路径:http:localhost:8080/工程名/test/t1
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/t1")
public String test(){
return "test";
}
}
3.RestFul风格
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
传统方式操作资源:通过不同的参数来实现不同的效果!方法单一,post和get。
使用Restful风格操作资源:可以通过不同的请求方法来实现不同的效果。
一般400的错误是请求出问题,500的错误是代码出问题。
package com.zhang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/t1")
public String test(int a, int b, Model model){
int c = a+ b;
model.addAttribute("msg","提交方式get,结果:"+c);
return "test";
}
}
` 传统方式:浏览器默认为get请求,请求地址:http://localhost:8080/t1?a=1&b=2
@Controller
public class TestController {
@RequestMapping(path="/t1", method= RequestMethod.GET)
public String test(int a, int b, Model model){
int c = a+ b;
model.addAttribute("msg","请求方式get,结果:"+c);
return "test";
}
}
Restful风格: 浏览器默认为get请求,请求地址:http://localhost:8080/t1/2/4
test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/t1/2/4" method="post">
${msg}
<br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
TestController.java
@Controller
public class TestController {
@RequestMapping(path="/t1/{a}/{b}", method= RequestMethod.GET)
public String test(@PathVariable int a, @PathVariable int b, Model model){
int c = a+ b;
model.addAttribute("msg","请求方式get,结果:"+c);
return "test";
}
@RequestMapping(path="/t1/{a}/{b}", method= RequestMethod.POST)
public String test2(@PathVariable int a, @PathVariable int b, Model model){
int c = a+ b;
model.addAttribute("msg","请求方式post,结果:"+c);
return "test";
}
}
请求结果:
submit提交设置为post请求,点击submit按钮,请求地址:http://localhost:8080/t1/1/2
两种请求方式,请求路径相同,请求方式不同,结果不同,这就是Restful风格。
优点:简洁,高效,安全(url不显示变量名)
小黄鸭调试法:向其他人提问或者解释遇到的问题,在解释的过程中自己想到了解决方案。
4.SpringMVC结果跳转方式
4.1ModelAndView 设置ModelAndView对象,根据view的名称,和视图解析器跳转到指定的页面。
页面路径:视图解析器前缀(prefix)+ viewName + 视图解析器后缀(suffix)
4.2Servlet 通过设置ServletAPI,不需要视图解析器。
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.zhang.controller"/>
<!--让SpringMVC不处理静态资源 .css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/>
<!--一句话自动完成DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例的注入(为了使用@RequestMapping注解)-->
<mvc:annotation-driven/>
<!--视图解析器-->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!–前缀–>
<property name="prefix" value="/WEB-INF/jsp/"/>
<!–后缀–>
<property name="suffix" value=".jsp"/>
</bean>-->
</beans>
HelloController.java
package com.zhang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping
public class HelloController {
@RequestMapping("/hello1")
public void hello1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//输出
resp.getWriter().println("Hello,Servlert");
}
@RequestMapping("/hello2")
public void hello2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 重定向
req.setAttribute("msg","hello2");
resp.sendRedirect("index.jsp");
}
@RequestMapping("/hello3")
public void hello3(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//转发
req.setAttribute("msg","heelo3");
req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
}
}
①(输出)请求地址:localhost:8080/hello1
结果:
②(重定向)请求地址:localhost:8080/hello2
结果:访问地址改变(不过好像只能访问WEN-INF目录下,子目录好像不能直接重定向访问)
③(转发)请求地址:localhost:8080/hello3
结果:
4.3 SpringMVC
4.3.1 通过SpringMVC来实现转发和重定向,无需视图解析器。(通过forward和redirect前缀来实现转发和重定向,不写默认为转发)
HelloController.java
package com.zhang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping
public class HelloController {
@RequestMapping("/hello1")
public String hello1(){
//转发
return "/WEB-INF/jsp/hello.jsp";
}
@RequestMapping("/hello2")
public String hello2(){
//转发
return "forward:/WEB-INF/jsp/hello.jsp";
}
@RequestMapping("/hello3")
public String hello3(){
//重定向
return "redirect:/index.jsp";
}
}
①(转发)请求路径:localhost:8080/hello1
结果:
②(转发)请求路径:localhost:8080/hello2
结果:
③(重定向)请求路径:localhost:8080/hello3
结果:
4.3.2 通过SpringMVC来实现转发和重定向,有视图解析器。
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.zhang.controller"/>
<!--让SpringMVC不处理静态资源 .css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/>
<!--一句话自动完成DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例的注入(为了使用@RequestMapping注解)-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
HelloController.java
package com.zhang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping
public class HelloController {
@RequestMapping("/hello")
public String hello1(){
//转发
return "hello";
}
}
请求地址:localhost:8080/hello
结果: