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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.monkey1024</groupId>
<artifactId>02mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- 与工程名一致 -->
<finalName>02mvc</finalName>
<plugins>
<!-- 编译插件,指定编译用的的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- jdk的版本号 -->
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--方式一:解决静态资源404的问题,将这些静态资源的请求交给DefaultServlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
-->
<!-- 字符编码过滤器 -->
<!-- 最好配置在其他过滤器之前,因为过滤器的执行顺序是根据web.xml中的顺序一致 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 手动指定字符编码 -->
<init-param>
<!-- 因为当前filter中的成员变量叫做encoding,所以此处必须也写成encoding -->
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!-- 强制指定字符编码,即使在request或者response中设置了字符编码,那么也会为其强制使用当前设置的字符编码 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 注册spring MVC中央控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<!-- spring MVC中的核心控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 如果配置了组件扫描器,并且有静态资源相关配置,需加这个并且,如果没有静态资源相关配置,可以不加 -->
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.monkey1024.*"/>
<!-- 方式三:解决静态资源无法访问的问题,添加下面标签即可-->
<!--
mapping:表示对该资源的请求,注意后面是两个*
location:静态资源所在的目录
-->
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<!-- 外部视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<!-- 定义外部资源view对象 -->
<bean id="monkey1024" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="http://www.monkey1024.com/"/>
</bean>
<!-- 内部视图解释类 需要放在controller上面-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 注册controller,
<bean id="/hello.do" class="com.monkey1024.controller.HelloSpringMVC" />
使用注解之后,就不用这个了 -->
</beans>
ReturnStringController.java
package com.monkey1024.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 方法中返回String类型
* 如果你只希望方法执行完毕后跳转jsp或其他资源,此时可以使用String作为方法的返回值
*/
@Controller
public class ReturnStringController {
/*
* 跳转到内部资源
*/
@RequestMapping("/welcome.do")
public String welcome() throws Exception{
return "welcome";
}
/*
* 跳转到外部资源
*/
@RequestMapping("/monkey1024.do")
public String monkey1024() throws Exception{
//此处返回的字符串需要跟springmvc.xml配置文件中外部资源view对象的bean的id保持一致
return "monkey1024";
}
}
ReturnVoidController.java
package com.monkey1024.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.monkey1024.bean.Student;
@Controller
public class ReturnVoidController {
/*
* 使用原生servlet中类
*/
@RequestMapping("/servletjump.do")
public void servletJump(HttpServletRequest request,HttpServletResponse response, Student student) throws ServletException, IOException{
request.setAttribute("student", student);
request.getRequestDispatcher("/jsp/welcome.jsp").forward(request,response);
}
}
welcome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎你来学习java<br>
${uername }<br>
${string }<br>
${student.name }<br>
${student.age }
</body>
</html>