栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

SpringMVC入门案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC入门案例

简单的SpringMVC案例 1.创建一个新的模块new->Module



2 .定义坐标



    4.0.0

    com.wym
    springmvc_01_quickStart
    1.0-SNAPSHOT
    war

    
        UTF-8
        1.8
        1.8
    


    
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
            org.springframework
            spring-webmvc
            5.2.10.RELEASE
        
    


    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.1
                
                    8080
                    /
                
            
        
    


3.定义Controller类
@Controller
public class UserController {
    //2.2、设置当前操作的访问路径
    @RequestMapping("/save")
    //2.3、设置当前操作的返回值
    @ResponseBody
    public String save(){
        System.out.println("user save~");
        return "{'name':'Helena'}";
    }
}
4. 创建SpringMVC的配置文件
//创建springmvc的配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.wym.controller")
public class SpringMvcConfig {

}
5. 定义Servlet容器启动的配置类
//定义一个servlet容器启动的配置类,在里面加载spring的配置
public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
    //加载SpringMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext act = new AnnotationConfigWebApplicationContext();
        act.register(SpringMvcConfig.class);
        return act;
    }

    //设置那些请求归属SpringMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

6. 配置

7. 启动后,在浏览器上输入地址,访问到save()方法的返回值;控制台输出相应内容。

Done!
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号