SpringMVC基础Demo搭建——annotation形式
来源:互联网 发布:unity3d lua 开发游戏 编辑:程序博客网 时间:2024/05/19 13:26
这里介绍SpringMVC基础Demo搭建的另一种形式:
一、所使用到的tools:
JDK 1.8: 下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
eclipse:Version: Luna Service Release 2 (4.4.2) 下载地址:
https://www.eclipse.org/downloads/
Tomcate:Apache Tomcat v7.0 下载地址:
https://tomcat.apache.org/download-70.cgi
maven: Apache Maven 3.3.9 下载地址:
http://maven.apache.org/download.cgi
SpringRELEASE: 4.2.0.RELEASE 下载地址:
http://repo.Spring.io/release/org/Springframework/Spring/
二、项目结构目录:
三、如何创建:
1、创建一个maven webapp项目
2、配置pom.xml
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>com.hzfstudy.springmvc</groupId> <artifactId>springmvc_annotation</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc_annotation Maven Webapp</name> <url>http://maven.apache.org</url> <!--设定spring版本,现在最新的是:4.2.4.RELEASE --> <properties> <springframework.version>4.2.4.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--添加spring依赖jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency> <!--添加 JSP/Servlet/Jstl依赖jar包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <!--声明关于maven的war插件--> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <warName>springmvc_annotation</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </pluginManagement> <finalName>springmvc_annotation</finalName> </build></project>
注意到,这里的pom.xml与在使用.xml方式配置的时候的不同点。
<1>、多了一项关于:maven-war-plugin ,因为在这里并不会使用任何xml配置文件,因此需要配置它来避免maven构建项目是失败。
<2>、多了关于JSP/Servlet/Jstl依赖是因为在代码中可能需要用到这些相关的API。
3、创建Controller类
HelloWorldController.java
package com.hzfstudy.springmvc.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping("/")public class HelloWorldController { @RequestMapping(method = RequestMethod.GET) public String HelloSpringMVC(ModelMap model) { model.addAttribute("Hello", "Hello SpringMVC"); return "helloSpringmvc"; } @RequestMapping(value="/hiSpringmvc", method = RequestMethod.GET) public String HiSpringMVC(ModelMap model) { model.addAttribute("Hi", "Hi SpringMVC"); return "hiSpringmvc"; }}
4、创建views
helloSpringmvc.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>HelloWorld page</title><style type="text/css">body { background-image: url('E:/qq.jpg');}</style></head><body> <br> <div style="text-align: center"> <h1>${Hello}</h1> <h3> <a href="http://localhost:8080/springmvc_annotation//hiSpringmvc">Go to HiSpringMVC </a> </h3> </div></body></html>
hiSpringmvc.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!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><style type="text/css">body { background-image: url('E:/ww.gif');}</style></head><body> <br> <div style="text-align: center"> <h1>${Hi}</h1> <h3> <a href="http://localhost:8080/springmvc_annotation/">Go to HelloSpringMVC</a> </h3> </div></body></html>
5、创建配置类:
HelloWorldConfiguration.java:
package com.hzfstudy.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 = "com.hzfstudy.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; }}
@Configuration :表明这个类包含一个或多个bean方法用@Bean注解,通过spring容器进行管理。这个类和上一篇的spring-servlet.xml对应。
@EnableWebMvc 对应xml文件中的 mvc:annotation-driven
@ComponentScan 对应xml文件中的context:component-scan base-package=”…”
HelloWorldInitializer.java
package com.hzfstudy.springmvc.configuration;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;public class HelloWorldInitializer implements WebApplicationInitializer { public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(HelloWorldConfiguration.class); ctx.setServletContext(container); ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); }}
上面的内容类似于web.xml中的内容。xml中,使用前端控制器DispatherServler,分配映射(在xml的url模式),而不是提供spring配置文件的路径(spring-servlet.xml),这里是通过注册配置类来实现。总的来说,是在做同样的事情,只是方法不同。
四、测试:
运行测试:
1.可以直接run as ->run on server 进行测试
2.也可以选择run as ->maven install,
BUILD SUCCESS 后将target文件中生成的springmvc_annotation.war文件放入Tomact下的webapps中,然后运行tomcat中的start.bat
最后在浏览器中输入访问的地址:
http://localhost:8080/springmvc_annotation/
http://localhost:8080/springmvc_annotation//hiSpringmvc
成功页面:
这就是关于SpringMVC的基础Demo搭建的俩种方式,后面将简单介绍下关于SpringMVC的REST风格。
- SpringMVC基础Demo搭建——annotation形式
- SpringMVC基础Demo搭建——XML形式
- SpringMVC基础Demo—创建RESTService
- SpringMvc环境搭建Demo
- 搭建springmvc项目(一)——基础搭建
- SpringMVC 解读——<mvc:annotation-driven/>
- SpringMVC 解读——<mvc:annotation-driven/>
- SpringMVC 解读——<mvc:annotation-driven/>
- SpringMVC—<mvc:annotation-driven/>标签
- Springmvc搭建简单测试demo
- 基础加强—注解(Annotation)
- JavaSE基础—Annotation(注解)
- SpringMVC框架的搭建(非注解形式)
- 简单的maven形式springmvc框架搭建
- SpringMVC基础框架搭建
- SpringMVC 的基础搭建
- SpringMVC+SpringJDBC搭建的项目Demo
- springmvc详解及demo的搭建
- 第1章第2节练习题14 判断子序列
- mybatis入门配置详解及实例下载
- Java IO流分析整理
- hbuider 框架分析
- win32窗口全屏
- SpringMVC基础Demo搭建——annotation形式
- easy-ui使用指南
- cvc-complex-type.2.4.d: 发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。
- java:IO流学习小结
- 正则表达式大全---进阶篇
- selenium处理HTML5的视频播放
- 【30天学习C++ primer系列】day2:基础拾忆之变量和基本类型(1/2)。-20160111
- datatable同结构和不同结构的合并
- 微信网页二维码授权登陆