Spring MVC 基础教程
来源:互联网 发布:买了域名之后怎么绑定 编辑:程序博客网 时间:2024/06/06 02:53
1. 创建一个简单的maven项目
2. 修改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> <groupId>com.spring</groupId> <artifactId>WebMvc</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- 基本属性 --> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- 其他web依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <!-- spring事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.9.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.6.v20170531</version> </plugin> </plugins> </build></project>
3. 在src/main/resources/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> <pre>Welcome to spring mvc world</pre></body></html>
4. spring mvc 配置
package com.amgji.config;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.view.InternalResourceViewResolver;@Configuration@EnableWebMvc@ComponentScan("com.amgji")public class MyMvcConfig { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; }}
5. Web配置
package com.amgji.config;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration.Dynamic;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(MyMvcConfig.class); ctx.setServletContext(servletContext); Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); }}
6. 简单控制器
package com.amgji.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController { @RequestMapping("/index") public String hello(){ return "index"; }}
7. 测试
执行maven命令:
mvn jetty:run
在浏览器中输入http://localhost:8080/index 进行测试
0 0
- Spring-MVC基础教程
- Spring MVC 基础教程
- Spring MVC 基础教程,快速入门,深入分析
- Spring MVC 基础教程,快速入门,深入分析
- J2EE基础教程:Spring 事务
- Spring基础教程网址
- Spring Cloud基础教程
- 拆,Spring Boot基础教程
- Spring Cloud 基础教程
- [转载]Spring Cloud基础教程
- [转载]Spring Boot基础教程
- spring cloud 学习基础教程
- Spring 基础教程之一:Spring简介
- spring mvc
- spring mvc
- spring mvc
- spring,MVC
- Spring MVC
- 第 8 章 查询语句详解
- 实验课中学到的小技巧
- 高并发Java 六 JDK并发包2
- JavaScript之模拟事件(事件)
- ubuntu创建新用户
- Spring MVC 基础教程
- OkRx扩展,让OkGo完美结合RxJava,比Retrofit更简单易用
- 在css选择器中:first-child与:first-of-type的用法
- JS实现tab栏的切换
- java读取word格式.doc或者.docx中的内容(APACHE POI)
- android视频录制MediaRecorder预览旋转角度问题
- test2
- jQuery插件layer扩展:解决大图显示时,图片的比例问题
- js多物体不同运动