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
<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>
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/"/>
<!-- 视图解释类 需要放在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>
Controller
package com.monkey1024.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/*
* 使用注解后就不需要实现接口了
*/
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC {
// 需要处理的url
// @RequestMapping("/test/test2.do")
// 只允许提交Post请求
// 方法名可以随意命名
@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持post请求");
// 连接到jsp
mv.setViewName("post");
return mv;
}
/*
* 以数组的形式,下面两个都可以调用这个方法
*/
// @RequestMapping({"/test/hello.do","/test/world.do"})
// @RequestMapping({"/hello.do","/world.do"})
// @RequestMapping(value="/test.do",params={"name","age"})
@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持get请求");
// 跳转到jsp
mv.setViewName("get");
return mv;
}
}
get.jsp
package com.monkey1024.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/*
* 使用注解后就不需要实现接口了
*/
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC {
// 需要处理的url
// @RequestMapping("/test/test2.do")
// 只允许提交Post请求
// 方法名可以随意命名
@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持post请求");
// 连接到jsp
mv.setViewName("post");
return mv;
}
/*
* 以数组的形式,下面两个都可以调用这个方法
*/
// @RequestMapping({"/test/hello.do","/test/world.do"})
// @RequestMapping({"/hello.do","/world.do"})
// @RequestMapping(value="/test.do",params={"name","age"})
@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持get请求");
// 跳转到jsp
mv.setViewName("get");
return mv;
}
}
post.jsp
package com.monkey1024.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/*
* 使用注解后就不需要实现接口了
*/
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC {
// 需要处理的url
// @RequestMapping("/test/test2.do")
// 只允许提交Post请求
// 方法名可以随意命名
@RequestMapping(value="/requestPost.do",method=RequestMethod.POST)
public ModelAndView reqPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持post请求");
// 连接到jsp
mv.setViewName("post");
return mv;
}
/*
* 以数组的形式,下面两个都可以调用这个方法
*/
// @RequestMapping({"/test/hello.do","/test/world.do"})
// @RequestMapping({"/hello.do","/world.do"})
// @RequestMapping(value="/test.do",params={"name","age"})
@RequestMapping(value="/requestGet.do",method=RequestMethod.GET)
public ModelAndView reqGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
// 类似键值对
mv.addObject("method","只支持get请求");
// 跳转到jsp
mv.setViewName("get");
return mv;
}
}
index.jsp
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World</h2>
<form action="/test/requestGet.do" method="get">
<!-- 通过属性params,必须携带的参数 -->
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" value="get请求">
</form>
<br>
<form action="/test/requestPost.do" method="post">
<input type="submit" value="post请求">
</form>
</body>
</html>