pom.xml 文件html
<?xml version="1.0" encoding="UTF-8"?> <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.zbj01</groupId> <artifactId>spring01</artifactId> <version>1.0</version> <packaging>war</packaging> <name>spring01</name> <!--依賴--> <dependencies> <!--Spring MVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.1.RELEASE</version> </dependency> <!--單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
</dependencies> <build> <finalName>${project.artifactId}</finalName> <testSourceDirectory>src/test/java</testSourceDirectory> <sourceDirectory>src/main/java</sourceDirectory> <!-- 處理沒法加載資源配置文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> <!--配置jetty servlet服務器--> <plugins> <!--配置maven編譯插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.12.v20180830</version> <configuration> <stopKey>exit</stopKey> <stopPort>8989</stopPort> <scanIntervalSeconds>10</scanIntervalSeconds> <reload>manual</reload> <webAppConfig> <contextPath></contextPath> </webAppConfig> <httpConnector> <port>80</port> </httpConnector> </configuration> <executions> <execution> <id>jetty-run</id> <phase>test</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
web.xml 文件java
<?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_4_0.xsd" version="4.0"> <display-name>spring01</display-name> <!--配置springmvc servlet--> <servlet> <servlet-name>smvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--以下配置,指定修改springmvc文件的名稱和位置,classpath* resources 目錄--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>smvc</servlet-name> <url-pattern>*.do</url-pattern> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
創建resources目錄/ springmvc.xml 文件web
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置handerAdapter 適配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 處理映射器 根據bean name 名稱進行查找 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 配置視圖渲染器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 將視圖名 渲染後視圖的前綴 --> <property name="prefix" value="/WEB-INF/template/"/> <!-- 渲染後視圖的後綴 --> <property name="suffix" value=".jsp"/> </bean> <bean class="com.zbj.controller.UserController" name="/user.action"></bean> <!-- 引入其它配置文件 --> <import resource="user.xml"/> </beans>
編寫控制器類
package com.zbj.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView("user");// 項目根目錄下 /WEB-INF/user.jsp
mv.addObject("name","張三丰");
mv.addObject("addr",new String[]{"鄭州","北京","天津"});
// mv.setViewName("WEB-INF/show"); //找/WEB-INF/show.jsp 渲染 /WEB-INF/show.jsp
return mv;
}
}
創建template目錄/user.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body><h3>${name}</h3><c:forEach items="${addr}" var="addr"> <p>${addr}</p></c:forEach></body></html>啓動服務器訪問 localhost/user.action