SpringMVC
Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架。
Spring:我们可以将Spring MVC所有要用到的bean 注册到Spring中
功能:RESTful风格、数据验证、格式化、本地化、主题等
Spring MVC以请求为驱动,围绕一个中心Servlet分派请求及提供其他功能,DispatchrServlet是一个实际的Servlet(继承自HttpServlet基类)
Spring MVC原理
实现方法一:
- 新建一个Moudle(模块),添加web的支持
- 确定导入了SpringMVC的依赖。
- 配置web.xml,注册DispatcherService
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置DispatcherServlet:这个是SpringMVC的核心:请求分发器,前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet要绑定SpringMvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
SpringMvc中,/ /*
/:只匹配所有的请求,不会去匹配jsp页面
/* :匹配所有的请求,包括jsp页面
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 在resources包中编写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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!-- BeanNameUrlHandlerMapping:bean -->
<bean id="/hello" class="com.kang.controller.HelloController"/>
</beans>
5.编写操作业务的Controller HelloController.class
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
//业务代码
String result="HelloSpringMVC";
mv.addObject("msg",result);
//视图跳转
mv.setViewName("hello");
return mv;
}
}
6.在/web/WEB-INF/jsp/hello.jsp 写要跳转的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
实现方法二:
- 新建一个Moudle(模块),添加web的支持
- 确定导入了SpringMVC的依赖。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 配置web.xml,注册DispatcherService
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置DispatcherServlet:这个是SpringMVC的核心:请求分发器,前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet要绑定SpringMvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
SpringMvc中,/ /*
/:只匹配所有的请求,不会去匹配jsp页面
/* :匹配所有的请求,包括jsp页面
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 配置resources资源包下面的 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.kang.controller"/>
<!--让Spring MVC不处理静态资源 .css .js .html .mp3-->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
帮助自动完成AnnotationMethodHandlerAdapter
和DefaultAnnotationHandlerMapping这两个实例的注入
-->
<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>
- 在WEB-INF包下面创建jsp包,可以保证视图安全,这个目录下的文件不能被客户端访问。
- HelloController .class
//代表会被Spring接管 被注解的这个类中的所有方法,如果返回值是String,并且有具体页面可以跳转,那么就会被视图解析器解析
@Controller
public class HelloController {
//真实访问地址: 项目名/HelloController/hello
@RequestMapping("/hello")
public String sayHello(Model model){
//向模型中添加属性msg与值,可以在jsp页面中取出并渲染
model.addAttribute("msg","hello,Spring MVC");
//web-inf/jsp/hello.jsp
return "hello";
}
}
- 创建视图层hello.jsp 在WEB-INF/jsp目录创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc-annotation</title>
</head>
<body>
${msg}
</body>
</html>
RestFul风格
localhost:8080/method/add
RestFul就是一个资源定位和资源操作的风格,基于这个风格设计的软件可以更加简洁、更有层次、更易于实现缓存的机制。
@Controller
public class RestFulController {
//传统方式: http://localhost:8080/s4/add?a=1&b=2
//RESTful风格: http://localhost:8080/s4/add/a/b
@RequestMapping("/add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model) {
int res = a + b;
model.addAttribute("msg", "结果为:" + res);
return "hello";
}
}
所有地址栏请求默认都会是 HTTP GET类型的
SpringMVC实现重定向和跳转
UserController
@Controller
public class ModelTest01{
@RequestMapping("/m1/t1")
//转发
public String test1() {
return "test";
}
@RequestMapping("/m1/t2")
//重定向
public String test2() {
//重定向
return "redirect:/index.jsp";
//return "redirect:hello.do"; //hello.do为另一个请求
}
}
JSON
JavaScript Object Notation, JS 对象标记
- JSON是一种轻量级的数据交换格式,使用广泛。
- 采用完全独立于编程的文本格式来存储和表示数据。
FastJson
fastjson.jar是阿里开发的一款专门用于Java开发的包,可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。