Spring MVC 配置和实例
来源:互联网 发布:手机淘宝店标制作教程 编辑:程序博客网 时间:2024/06/11 00:25
1、配置Maven,加入Spring mvc的依赖。网址:http://mvnrepository.org/
2、加入servlet-api的jar包。
3、配置web.xml 和spring-mvc.xml
4、写controller类。
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.bb</groupId> <artifactId>bb</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>bb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.9.RELEASE</version> </dependency> <!-- 添加junit4依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 指定范围,在测试时才会加载 --> <scope>test</scope> </dependency> </dependencies> <build> <finalName>bb</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </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_3_0.xsd" version="3.0"> <display-name>bb</display-name> <!-- spring mvc servlet--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处也可以配置成 *.do 形式 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list></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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 @Controller--> <context:component-scan base-package="com.controller;"/></beans>
controller.java
package com.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Controllerpublic class UserController { @RequestMapping("helloworld") public void helloworld(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.getWriter().write("welcome"); }}
0 0
- Spring MVC 配置和实例
- 【Spring学习笔记七】-Spring MVC基本配置和实例
- spring mvc 简介和实例
- Spring MVC第一个简单配置实例
- Spring MVC 配置 druid 数据源实例
- Spring MVC+Maven+Velocity配置实例
- Eclipse中spring mvc配置实例详解
- Spring MVC 配置 druid 数据源实例
- Spring MVC 配置 druid 数据源实例
- Spring MVC拦截器配置实例
- Spring MVC和Spring配置AOP
- spring mvc 和ajax异步交互实例
- Spring MVC和AJAX异步交互实例
- Spring MVC中,基于XML配置和基于注解的依赖注入实例
- spring WEB MVC + Spring + hibernate开发web程序的配置说明和简单实例的详细说明
- 【spring mvc】spring-mvc配置
- 通过spring context和spring MVC context获取bean实例
- spring mvc 异常处理和Log4j配置
- hadoop之hdfs和yarn
- Java中标准输入操作的几点总结
- 关于Activity之间传递数据的6种方式
- 1483 区间价值
- go语言-文件夹复制
- Spring MVC 配置和实例
- Java实现图片验证码
- 安装jenkins
- CodeForces 106C 【DP】
- 《ACM程序设计》书中题目
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- hdu 2112 HDU Today(floyd 算法)
- Dubbox 编译
- android网络图片查看器