添加Maven依赖:
<!-- spring核心类 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
添加tomcat7插件到maven:
<!--构建--> <build> <plugins> <!-- 插件配置 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <port>8080</port> <path>/springMVC</path> </configuration> <version>2.2</version> </plugin> </plugins> </build>
FirstController.java
package com.xiaobiti.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //设置当前类为处理器的类 @Controller public class FirstController { //设定当前方法的访问映射地址 @RequestMapping("firstController") public String hello(){ System.out.println("访问到了firstController"); //设置跳转页面 return "/success"; } }
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置springMVC需要扫描的包 --> <context:component-scan base-package="com.xiaobiti.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/pages"/> <property name="suffix" value=".jsp"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始化参数,读取Spring MVC的配置文件 --><init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
success.jsp
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello SpringMVC </body> </html>
index.jsp
<html> <body> <h2>Hello World!</h2> <a href="firstController">Link</a> </body> </html>
点击Link后访问firstController,返回跳转页面success.jsp
标签:success,spring,程序,springframework,MVC,firstController,Spring,org,DispatcherServl From: https://www.cnblogs.com/xiaobiti/p/17302494.html