码迷,mamicode.com
首页 > 编程语言 > 详细

SSM-java配置

时间:2018-10-21 10:17:28      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:XML   snapshot   set   ssm   version   class   star   val   技术   

<?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配置

标签:XML   snapshot   set   ssm   version   class   star   val   技术   

原文地址:https://www.cnblogs.com/a103007/p/9823821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!