SSM-java配置

Posted a103007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM-java配置相关的知识,希望对你有一定的参考价值。

<?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>com.xpl</groupId>
    <artifactId>springBootMV</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>
package com.xpl.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(value="com.xpl",useDefaultFilters = true,
        excludeFilters =  @ComponentScan.Filter(type=FilterType.ANNOTATION,value={Controller.class}))
public class SpringConfig {
}
package com.xpl.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(value="com.xpl",useDefaultFilters = false,
        includeFilters = @ComponentScan.Filter(type=FilterType.ANNOTATION,value={Controller.class,Configuration.class}))
public class SpringMVCConfig extends WebMvcConfigurationSupport {
    @Override
    public ViewResolver mvcViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
    }
}
package com.xpl.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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;

public class WebInit implements WebApplicationInitializer {
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
        cxt.register(SpringMVCConfig.class);
        cxt.setServletContext(servletContext);
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(cxt));
        springmvc.addMapping("/");
        springmvc.setLoadOnStartup(1);

    }
}
package com.xpl.controller;

import com.xpl.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class Hello {
    @Autowired
    HelloService helloService;
    @RequestMapping("/hello")
    public String hello(Model model){
        String res = helloService.setHello();
        Map map = new HashMap<String, String>();
        map.put("msg",res);
        model.addAttribute("msg",map);
        return "msg";
    }
}
package com.xpl.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
    public String setHello(){
        return "xupeilei";
    }
}
<%--
  Created by IntelliJ IDEA.
  User: xpl
  Date: 2018/10/21
  Time: 2:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

 技术分享图片

 

以上是关于SSM-java配置的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置

VS Code配置markdown代码片段

VS Code配置markdown代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段