SpringMVC入门案例(注解)
使用普遍的方式@Controller主耳机形式
入门案例:
这里我将采用最简单的配置进行springmvc开发
步骤:
文档结构:
C:.
│ pom.xml
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─lifeibai
│ │ └─web
│ │ └─controller
│ │ HelloController.java ---Controller
│ │
│ ├─resources
│ │ spring-mvc.xml---这里只需要配置Spring注解扫描即可
│ │
│ └─webapp
│ │ index.jsp
│ │
│ └─WEB-INF
│ web.xml---配置前端控制器和配置springmvc的配置文件位置
│
└─test
└─java
pom.xml
?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"
modelVersion4.0.0/modelVersion
groupIdcom.lifeibai.springmvc/groupId
artifactIdspringmvc_demo_002/artifactId
version1.0-SNAPSHOT/version
packagingwar/packaging
dependencies
!--jsp标签库--
dependency
groupIdjstl/groupId
artifactIdjstl/artifactId
version1.2/version
/dependency
!--日志管理--
dependency
groupIdcommons-logging/groupId
artifactIdcommons-logging/artifactId
version1.2/version
/dependency
!--aop联盟--
dependency
groupIdaopalliance/groupId
artifactIdaopalliance/artifactId
version1.0/version
/dependency
!--核心--
dependency
groupIdorg.springframework/groupId
artifactIdspring-core/artifactId
version4.2.8.RELEASE/version
/dependency
!--aspects--
dependency
groupIdorg.springframework/groupId
artifactIdspring-aspects/artifactId
version4.2.8.RELEASE/version
/dependency
!--aop--
dependency
groupIdorg.springframework/groupId
artifactIdspring-aop/artifactId
version4.2.8.RELEASE/version
/dependency
!--tx事务--
dependency
groupIdorg.springframework/groupId
artifactIdspring-tx/artifactId
version4.2.8.RELEASE/version
/dependency
!--容器管理--
dependency
groupIdorg.springframework/groupId
artifactIdspring-beans/artifactId
version4.2.8.RELEASE/version
/dependency
!--context 好像跟扫描包有关系--
dependency
groupIdorg.springframework/groupId
artifactIdspring-context/artifactId
version4.2.8.RELEASE/version
/dependency
!--表达式语言--
dependency
groupIdorg.springframework/groupId
artifactIdspring-expression/artifactId
version4.2.8.RELEASE/version
/dependency
!--spring web下的工具包--
dependency
groupIdorg.springframework/groupId
artifactIdspring-web/artifactId
version4.2.8.RELEASE/version
/dependency
!--spring mvc--
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version4.2.8.RELEASE/version
/dependency
!--测试包--
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version4.12/version
/dependency
!--spring整合测试--
dependency
groupIdorg.springframework/groupId
artifactIdspring-test/artifactId
version4.2.8.RELEASE/version
/dependency
!--ServletAPI编译时期--
dependency
groupIdjavax.servlet/groupId
artifactIdservlet-api/artifactId
version2.5/version
scopeprovided/scope
/dependency
/dependencies
build
plugins
plugin
groupIdorg.apache.tomcat.maven/groupId
artifactIdtomcat7-maven-plugin/artifactId
version2.2/version
/plugin
/plugins
/build
/project
web.xml
?xml version="1.0" encoding="UTF-8"?
web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"
servlet
servlet-nameaction/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
init-param
param-namecontextConfigLocation/param-name
param-valueclasspath:spring-mvc.xml/param-value
/init-param
load-on-startup1/load-on-startup
/servlet
servlet-mapping
servlet-nameaction/servlet-name
url-pattern*.do/url-pattern
/servlet-mapping
/web-app
spring-mvc.xml
?xml version="1.0" encoding="UTF-8"?
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
context:component-scan base-package="com.lifeibai"/
/beans
HelloController.java
package com.lifeibai.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Author:Alan
*
* @Date: 2018-05-21 21:38:14
*/
@Controller
public class HelloController {
@RequestMapping("hi")
public String hi(){
return "index.jsp";
}
}
index.jsp
%@ page contentType="text/html;charset=UTF-8" language="java" %
html
head
titleTitle/title
/head
body
h1Hello Spring MVC/h1
/body
/html
测试结果
http://localhost:8080/springmvc_demo_002/hi.do
是不是感觉超简单!
文章如有错误,请您一定指出,感谢之至!
如果你有不同的见解,欢迎留言,或者加我QQ986320270
图片可能来源于网络,如有侵权请告知。
最后:关注一下呗
如果你有不同的见解,欢迎留言,或者加我QQ986320270
最后:关注一下呗
长按二维码识别关注