HelloWorld
导入依赖
比Spring多了web和webmvc
1 | <dependency> |
2 | <groupId>org.springframework</groupId> |
3 | <artifactId>spring-web</artifactId> |
4 | <version>5.2.3.RELEASE</version> |
5 | </dependency> |
6 | <dependency> |
7 | <groupId>org.springframework</groupId> |
8 | <artifactId>spring-webmvc</artifactId> |
9 | <version>5.2.3.RELEASE</version> |
10 | </dependency> |
添加Tomcat
Edit Configuration–>左上角” + “号–>选择Tomcat Server–>local
右侧server标签卡里:Application server右边配置tomcat根目录–>OK
web.xml
1 | |
2 | <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" |
3 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
4 | xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> |
5 | <!--<!DOCTYPE web-app PUBLIC--> |
6 | <!-- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"--> |
7 | <!-- "http://java.sun.com/dtd/web-app_2_3.dtd" >--> |
8 | <!--<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">--> |
9 | <display-name>Archetype Created Web Application</display-name> |
10 | <servlet> |
11 | <servlet-name>springmvc</servlet-name> |
12 | <!--servlet-class中的值是spring-webmvc包提供的类,即前端控制器,用于控制所有请求 --> |
13 | <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
14 | <init-param> |
15 | <param-name>contextConfigLocation</param-name> |
16 | <param-value>classpath:spring-mvc.xml</param-value> |
17 | </init-param> |
18 | <load-on-startup>1</load-on-startup> |
19 | </servlet> |
20 | <servlet-mapping> |
21 | <servlet-name>springmvc</servlet-name> |
22 | <!--url-pattern(重点)中有3个值,分别为/、 /*、 *.action --> |
23 | <url-pattern>/</url-pattern> |
24 | </servlet-mapping> |
25 | </web-app> |
springmvc.xml
1 | |
2 | <beans xmlns="http://www.springframework.org/schema/beans" |
3 | xmlns:mvc="http://www.springframework.org/schema/mvc" |
4 | xmlns:context="http://www.springframework.org/schema/context" |
5 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
6 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
7 | http://www.springframework.org/schema/beans/spring-beans.xsd |
8 | http://www.springframework.org/schema/mvc |
9 | http://www.springframework.org/schema/mvc/spring-mvc.xsd |
10 | http://www.springframework.org/schema/context |
11 | http://www.springframework.org/schema/context/spring-context.xsd"> |
12 | |
13 | <context:component-scan base-package="com.czw.controller"></context:component-scan> |
14 | |
15 | </beans> |
JSP页面
index.jsp(webapp下),success.jsp(webapp/WEB-INF/pages/success.jsp)
1 | <a href="hello">跳转页面到helloworld</a> |
控制器Controller
1 | |
2 | public class HelloController { |
3 | ("/hello") |
4 | public String hello(){ |
5 | System.out.println("处理hello请求。。。"); |
6 | return "/WEB-INF/pages/success.jsp"; |
7 | } |
8 | } |
点击超链接跳转成功
对比Spring Boot
选择web模块
写两个页面
放在resources/static下
配置视图控制器
如果用了thymeleaf就不用配了,在templates下建文件,自动带上前后缀。
1 | |
2 | public class HelloConfig implements WebMvcConfigurer { |
3 | |
4 | public void addViewControllers(ViewControllerRegistry registry) { |
5 | registry.addViewController("/").setViewName("/hello.html"); |
6 | registry.addViewController("/world").setViewName("/world.html"); |
7 | } |
8 | } |
控制层Controller
1 | |
2 | public class HWController { |
3 | |
4 | ("/hello") |
5 | public String hello(){ |
6 | System.out.println("处理Hello请求"); |
7 | return "/world"; |
8 | } |
9 | } |
完善
视图解析器
springmvc.xml里
1 | <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
2 | <property name="prefix" value="/WEB-INF/pages/"/> |
3 | <property name="suffix" value=".jsp"/> |
4 | </bean> |
总结
头发-1