`
wiselyman
  • 浏览: 2080538 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81059
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130118
社区版块
存档分类
最新评论

01点睛Spring MVC 4.1-搭建环境

 
阅读更多

1.1 简单示例

  • 通篇使用java config
  • @Controller声明bean是一个控制器
  • @RequestMapping访问路径和方法之间的映射

1.2 演示

1.2.1 新建maven web项目

  • 新建项目 

1.2.2 添加spring mvc依赖到maven

pom.xml修改如下

<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>
    <artifactId>testSpringMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <!-- Generic properties -->
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>
        <!-- Spring -->
        <spring-framework.version>4.1.5.RELEASE</spring-framework.version>
        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback.version}</version>
        </dependency>

    </dependencies>



    <groupId>com.wisely</groupId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


1.2.3 构建目录

  • 删除src/main/webapp/WEB-INF/web.xml;
  • 删除src/main/resources下的除logback.xml;
  • logback.xml添加<logger name="org.springframework.web" level="DEBUG"/>,观察请求错误(4xx错误);
  • src/main/java新建packagecom.wisely;
  • 目录结构如图 

1.2.4 基于java config的spring mvc的配置

本例基于java config,包括不使用web.xml

  • Spring MVC的配置
package com.wisely;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.wisely")
@EnableWebMvc
public class DemoMVCConfig extends WebMvcConfigurerAdapter {

    //名称与实际的页面的映射
    // return "index" ; 实际返回的页面是/WEB-INF/views/index.jsp
    @Bean
    public UrlBasedViewResolver viewResolver(){
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
          resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}
  • WebInitializer(相当于web.xml)
package com.wisely;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.jsp.jstl.core.Config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(DemoMVCConfig.class);
            //注册spring mvc的DispatcherServlet
            ctx.setServletContext(servletContext);
            Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
            servlet.addMapping("/");
            servlet.setLoadOnStartup(1);

    }

}

1.2.5 简单的Controller

  • HomeController
package com.wisely.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping("/")
    public String home(){
        return "index";
    }
}

  • src/main/webapp/WEB-INF/views/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
index.jsp  is here
</body>
</html>

1.2.6 运行

 

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


1
1
分享到:
评论
11 楼 loveminer 2016-02-05  
/web/src/main/webapp/WEB-INF/mvc-config.xml这个要不要删除?
10 楼 loveminer 2016-02-05  
这个配置全了吗,为何我的容器没初始化,不用配置文件它是怎么加载的??有点不是很理解
9 楼 xing_kenny 2015-10-23  
我今天把过程在STS(3.7.0.RELEASE)里又走了一遍,这次成功了!!!
8 楼 xing_kenny 2015-10-22  
@Controller
public class HomeController {
    @RequestMapping("/")
    public String home(){
        return "index";
    }
}

如果改成

@Controller
@RequestMapping({"/home"})
public class HomeController {

    @RequestMapping("")
    public String home(){
    System.out.println("------ this is HomeController");
        return "index";
    }
}

当发起请求 http://localhost:8080/testSpringMVC/home

也就是Controller的声明没有生效,请问怎样解决?
7 楼 xing_kenny 2015-10-22  
明显执行不了
6 楼 wiselyman 2015-10-16  
琥珀川lesson 写道
楼主可以整理成文档形式提供附件下载吗?

已经整理成书,内容是目前的好多倍
5 楼 琥珀川lesson 2015-10-06  
楼主可以整理成文档形式提供附件下载吗?
4 楼 zhangbaocheng 2015-07-03  
有些代码显示不全
3 楼 s524141771 2015-07-02  
请问楼主 是在maven中配置的tomcat插件运行的,还是直接在项目右击-->run as -->run as on server -->选择tomcat 运行的啊?
这边启动不了,麻烦解答下,谢谢!!!
2 楼 wiselyman 2015-05-29  
hyneng 写道
jetty启动无效

我只在tomcat下作过测试
1 楼 hyneng 2015-05-29  
jetty启动无效

相关推荐

Global site tag (gtag.js) - Google Analytics