spring mvc: 注解和JavaConfig实例
通过javaConfig来配置config,并能正常访问url。
先看图
访问地址:http://localhost:8080/gugua5/
http://localhost:8080/gugua5/helloagain
先看下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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gugua4</groupId>
<artifactId>gugua5</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>gugua5 Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies> <!-- spring-test支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- spring模块库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${springVersion}</version>
</dependency> <!-- (aop)@Aspect注解及代理 -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib-nodep -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency> <!-- 依赖的持久化类库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <!-- io流/上传类插件 -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency> <!-- 连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commonsDbcpVersion}</version>
</dependency> <!-- 公共基础类(字符处理,数组,日期,范围) -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency> <!-- jsp依赖的web模块库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency> <!-- servlet(HttpServletRequest,HttpServletResponse) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!--
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
-->
</dependency> </dependencies> <build>
<!-- javaConfig配置 -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- jsp目录 -->
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>gugua5</warName>
<!-- 取消xml配置:web.xml -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>gugua5</finalName>
</build> <properties>
<springVersion>4.3.5.RELEASE</springVersion>
<mysqlVersion>5.0.11</mysqlVersion>
<commonsDbcpVersion>1.4</commonsDbcpVersion>
<aspectjweaverVersion>1.8.13</aspectjweaverVersion>
<commonsLoggingVersion>1.2</commonsLoggingVersion>
</properties> </project>
额外说下,build配置
首先要注意这里maven-war-plugin 插件的声明。正如我们将完全删除web.xml ,我们需要配置这个插件,以避免Maven构建war包失败。第二个变化是加入了JSP/Servlet/Jstl 的依赖关系,这些我们可能需要,因为我们将要使用 servlet API和JSTL视图在我们的代码中.
<build>
<!-- javaConfig配置 -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- jsp目录 -->
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<!-- 项目名/url -->
<warName>gugua5</warName>
<!-- 取消xml配置:web.xml/applicationContext.xml/xxx-servlet.xml -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>gugua5</finalName>
</build>
HelloController.java
package springmvc.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap; @Controller
@RequestMapping(value="/")
public class HelloWorldController { @RequestMapping(method=RequestMethod.GET)
public String sayHello(ModelMap model)
{ model.addAttribute("greeting", "hello world from spring mvc 4");
return "hello_say";
} @RequestMapping(value="/helloagain", method=RequestMethod.GET)
public String sayHelloAgain(ModelMap model)
{
model.addAttribute("greeting", "hello world again, spring mvc 4");
return "hello_say";
}
}
添加配置类
HelloWorldConfiguration.java
在src/main/java下添加下面提到的类指定的包,如下图所示。这种构造类可以被看作是一个替代 spring-servlet.xml,因为它包含了所有必需的组件的扫描和视图解析器的信息。
@Configuration指明该类包含注解为@Bean 生产 bean管理是由Spring容器的一个或多个bean方法。
@EnableWebMvc 等同于 mvc:annotation-driven 在XML中. 它能够为使用@RequestMapping向特定的方法传入的请求映射@Controller-annotated 类。
@ComponentScan 等同于 context:component-scan base-package="..." 提供 spring 在哪里寻找 管理 beans/classes.
package springmvc.configuration; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; @Configuration
@EnableWebMvc
@ComponentScan(basePackages="springmvc")
public class HelloWorldConfiguration { @Bean
public ViewResolver viewResolver()
{
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
} }
添加一个初始化类实现 WebApplicationInitializer 在src/main/java 中使用如下图所示指定包(在这种情况下,作为替代在 web.xml 中定义的任何 Spring 配置)。在Servlet 3.0的容器启动时,这个类将被加载并初始化,并在启动由servlet容器调用方法。
package springmvc.configuration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { HelloWorldConfiguration.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
} @Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String [] { "/" };
} }
https://www.yiibai.com/spring_mvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example.html
spring mvc: 注解和JavaConfig实例的更多相关文章
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc 注解入门示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- spring mvc 注解示例
springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- 关于Spring mvc注解中的定时任务的配置
关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...
- spring mvc 注解@Controller @RequestMapping @Resource的详细例子
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring+Spring MVC+Hibernate框架搭建实例
前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- Spring4 MVC HelloWorld 注解和JavaConfig实例
在这一节中,我们以 Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 在先前的 Sp ...
- spring mvc:练习:javaConfig配置和注解
Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 我们已经使用XML配置开发了一个H ...
随机推荐
- lnmp1.4环境下thinkphp3.2配置pathinfo模式
1.打开php.ini 通常该文件在 /usr/local/php/etc/php.ini vi /usr/local/php/etc/php.ini 找到 cgi.fix_pathinfo,默认为0 ...
- media query媒体查询
媒体查询(CSS3 media query) 一.逻辑操作符:not.and.only not:not操作符用来对一条媒体查询的结果取反. and:and操作符用来把多个媒体属性组合起来,合并到同一条 ...
- keeplived + mysql双主复制部署 --原创
环境: master 1: 192.168.100.10 oracle linux 7.4 mysql 5.7.1 master 2: 192.168.100.11 oracle linux ...
- @FindBy、@FindBys、@FindAll的区别
原文地址http://blog.csdn.net/tea_wu/article/details/21080789 selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是 ...
- Linux系统——vim编辑器
一.Linux系统中最常用的文本编辑器 vi:类Unix系统中默认的文本编辑器 vim:vi编辑器的增强版 作用:创建或修改文本文件:维护Linux系统中各种配置文件 二.三种切换模式命令模式输入模式 ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- web前端基础——初识JavaScript
1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...
- 裁剪TOGAF进行产品架构开发
http://ea.zhoujingen.cn/56.html . 有人和我说“周老师,我的企业条件不适合,学习企业架构没用.” 如果等公司让我用企业架构,估计会比我自己开始晚七八年.我们学习任何内容 ...
- springCloud--1
电影微服务是服务消费者,用户微服务是服务提供者. Springcloud对eureka的支持很好,eureka本身是一个基于REST的服务, Eureka里面有一个注册表,Application Cl ...
- 基于Python的接口测试框架实例
文章来源:http://www.jb51.net/article/96481.htm 下面小编就为大家带来一篇基于Python的接口测试框架实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考. ...