搭建ssm项目(以java的方式)

1.项目结构

在这里插入图片描述

2.spring的配置

package com.yl.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

/**
 * @Author: wfj
 * @Description: 这个配置类相当于spring的applicationContext文件,除了controller以外,com.yl包下的所有文件都扫描
 * @Date: created in 2021/9/2 23:47
 * @Version: 1.0v
 */
@Configuration
@ComponentScan(basePackages = "com.yl",useDefaultFilters = true,excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringConfig {
}

3.springmvc的配置

package com.yl.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: wfj
 * @Description: 这个配置类相当于springmvc的配置,只扫描controller
 * @Date: created in 2021/9/2 23:51
 * @Version: 1.0v
 */
@Configuration
@ComponentScan(basePackages = "com.yl",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)})
public class SpringMVCConfig extends WebMvcConfigurationSupport {

    /**
     * 放行静态资源
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/**").addResourceLocations("classpath:/");
    }

    /**
     * 配置视图解析器
     * @param registry
     */
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
       registry.jsp().prefix("/jsp/");
       registry.jsp().suffix(".jsp");

    }

    /**
     * 配置视图控制器
     * @param registry
     */
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/hello3","hello");
    }

    /**
     * 配置消息转换器
     * @param converters
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
        converters.add(converter);

    }
}

4.web.xml的配置

package com.yl.config;

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;

/**
 * @Author: wfj
 * @Description: 这个配置类类似于web.xml文件
 * @Date: created in 2021/9/2 23:54
 * @Version: 1.0v
 */
public class WebInit implements WebApplicationInitializer {

    @Override
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMVCConfig.class);
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
        springmvc.addMapping("/");
        springmvc.setLoadOnStartup(1);

    }
}

5.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.example</groupId>
    <artifactId>java-ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!--springmvc依赖(包含了spring的依赖)-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!--servlet的依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!--jsp解析用到的依赖-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!--json依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.3</version>
        </dependency>
    </dependencies>


</project>

6.实体类

package com.yl.entity;

import java.util.Date;

/**
 * @Author: wfj
 * @Description:
 * @Date: created in 2021/9/4 3:53
 * @Version: 1.0v
 */
public class User {
    private String name;
    private int age;
    private Date birth;

    public User() {
    }

    public User(String name, int age, Date birth) {
        this.name = name;
        this.age = age;
        this.birth = birth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

7.接口

package com.yl.service;

/**
 * @Author: wfj
 * @Description:
 * @Date: created in 2021/9/3 0:09
 * @Version: 1.0v
 */
public interface HelloService {

    String hello();
}

8.接口实现类

package com.yl.service.impl;

import com.yl.service.HelloService;
import org.springframework.stereotype.Service;

/**
 * @Author: wfj
 * @Description:
 * @Date: created in 2021/9/3 0:09
 * @Version: 1.0v
 */
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello() {
        return "java ssm...";
    }
}

9.controller

package com.yl.controller;

import com.yl.entity.User;
import com.yl.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: wfj
 * @Description:
 * @Date: created in 2021/9/2 23:46
 * @Version: 1.0v
 */
@Controller
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return helloService.hello();
    }

    @GetMapping("/hello2")
    public String hello2() {
        return "hello";
    }

    @GetMapping("/names")
    @ResponseBody
    public List<String> getNames() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("xiaolan"+i);
        }
        return list;
    }

    @GetMapping("/getUserNames")
    @ResponseBody
    public List<User> getUserNames() {
        List<User> list = new ArrayList<>();
        for (int i = 0; i< 10; i++) {
            list.add(new User("xiaobai" + i,18,new Date()));
        }
        return list;
    }
}

10.getUserNames接口运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值