-
使用xml方式搭建ssm框架
- 第一步:创建maven工程,设置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lyl</groupId>
<artifactId>xmlssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
- 第二步:创建applicationContext.xml、springmvc.xml、web.xml
- spring的配置文件:applicationContext.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"
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">
<!--use-default-filters:使用默认的过滤器,扫描org.lyl包下的所有东西-->
<context:component-scan base-package="org.lyl" use-default-filters="true">
<!--除去org.lyl.controller包下的内容-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
- springmvc的配置文件:springmvc.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
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--use-default-filters:false表示一个都不扫描-->
<context:component-scan base-package="org.lyl" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--开启springmvc注解的支持-->
<mvc:annotation-driven/>
</beans>
- web.xml
<?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_3_1.xsd"
version="3.1">
<!--spring与springmvc整合所需配置(context-param、listener)-->
<!--设置spring配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring的监听器
ContextLoaderListener:默认只加载applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置视图解析器,与springmvc相关的配置-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 第三步:编写controller、service
- controller:
package org.lyl.controller;
import org.lyl.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
/*produces = "text/html;charset=utf-8":设置编码,让中文不乱码*/
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
public String hello() {
return this.helloService.sayHello();
}
}
- service
package org.lyl.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String sayHello() {
return "hello lyl 好好学习呀!";
}
}
- 第四步:配置tomcat运行
运行路径:localhost:8080/hello
- 使用java方式搭建ssm框架
- 第一步:搭建maven工程,配置pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
- 第二步:创建SpringConfig.java、SpringMvcConfig.java、WebInit.java
- SpringConfig.java
package org.lyl.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = "org.lyl",useDefaultFilters = true, excludeFilters =
{@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
public class SpringConfig {
}
- SpringMvcConfig.java
package org.lyl.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = "org.lyl",useDefaultFilters = false,includeFilters =
{@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)})
public class SpringMvcConfig {
}
- WebInit.java
package org.lyl.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
@Configuration
public class InitWeb implements WebApplicationInitializer{
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext acw = new AnnotationConfigWebApplicationContext();
acw.setServletContext(servletContext);
acw.register(SpringMvcConfig.class);
ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(acw));
springmvc.addMapping("/");
springmvc.setLoadOnStartup(1);
}
}
- 第三步:编写controller和server
- controller
package org.lyl.controller;
import org.lyl.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
public String hello() {
return helloService.sayHello();
}
- service
package org.lyl.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String sayHello() {
return "hello lyl 加油呀";
}
}
- 第四步:tomcat运行
运行路径 :localhost:8080/hello
- 在java代码编写的ssm框架中添加内容
- 静态资源过滤
- pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
- 在SpringMvcConfig.java中添加
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
public class SpringMvcConfig extends WebMvcConfigurationSupport{
/**
* 配置静态资源过滤,要先让类实现WebMvcConfigurationSupport
* @param registry
* 访问:localhost:8080/static/index.html(**)
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
- 拦截器
- MyInterceptor
package org.lyl.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
- SpringMvcConfig.java中添加
import org.lyl.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
/**
* 配置拦截器
* @param registry
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
}
@Bean
MyInterceptor myInterceptor() {
return new MyInterceptor();
}
- 集合转json格式
- pom.xml
<!--json格式-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
- controller中添加
@GetMapping("/data")
public List<String> getData() {
ArrayList<String> list = new ArrayList<String>();
for(int i =0;i < 10;i++) {
list.add("hello lyl---" + i);
}
return list;
}
- SpringMvcConfig.java中添加
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
/**
* 配置集合数据转换成json格式
* @param converters
* 访问:localhost:8080/data
*/
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
converters.add(convert);
}