<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency>
package com.zb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String haha(){ System.out.println("Controller"); //指定到WEB-INF/jsp下的success.jsp文件 return "success"; } }
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 组件扫描 扫描注解--> <context:component-scan base-package="com.zb.controller"/> <!-- 视图解析器 resourceView 在Controller注解的类的方法中收到返回值 再将返回值与前缀prefix的值WEB-INF/jsp/拼接 与suffix的值.jsp拼接 --> <bean id="resourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:annotation-driven/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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"> <servlet> <!--dispatcherServlet前置控制器 MVC C层核心控制器--> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- dipatcherServlet加载springIOC容器--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--1指在web容器加载时 就加载dispatcherServlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!--/ 表示dispatcherServlet拦截所有用户请求url 除了后缀为.jsp的文件url--> <!-- /* 表示拦截所有文件url--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>success</title> </head> <body> <h1>hello world!!!!haha</h1> </body> </html>
<html> <body> <a href="hello"> <h2>Hello World!</h2></a> </body> </html>
原文:https://www.cnblogs.com/codefarmer-zb/p/15110176.html