1、新建一个maven的web项目,选择
maven-archetype-webapp
2、pom.xml文件加入依赖
spring-webmvc依赖,间接把spring的依赖都加入到项目;jsp,servlet依赖
- <properties>
-
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
-
- <maven.compiler.source>1.8maven.compiler.source>
-
- <maven.compiler.target>1.8maven.compiler.target>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.2.5.RELEASEversion>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
-
- dependencies>
-
- <build>
- <resources>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
-
- <filtering>falsefiltering>
- resource>
- resources>
- build>
3、新建web.xml文件,并在该配置文件中注册SpringMVC框架的核心对象DispatcherServlet
- "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">
-
-
-
- <servlet>
- <servlet-name>mywebservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springmvc.xmlparam-value>
- init-param>
-
-
-
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>mywebservlet-name>
-
-
- <url-pattern>*.dourl-pattern>
- servlet-mapping>
-
- web-app>
4、创建一个发起请求的页面 index.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <p>SpringMVC项目p>
- <p> <a href="menu/some.do">发起some.do的GET请求a> p>
- <br/>
-
- <form action="menu/other.do" method="post">
- <input type="submit" value="post请求other.do">
- form>
- <br/>
-
- <p> <a href="menu/noMethod.do">发起noMethod.do的GET请求a> p>
- <br/>
-
- <form action="menu/noMethod.do" method="post">
- <input type="submit" value="post请求noMethod.do">
- form>
- body>
- html>
5、创建控制器(处理器)类
6、创建一个结果的 show.jsp,显示处理结果
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <h3>/WEB-INF/view/show.jsp从request作用域获取数据h3>
- <h3>msg数据:${msg}h3><br/>
- <h3>fun数据:${fun}h3>
- body>
- html>
7、创建SpringMVC的配置文件(和spring的配置文件一样)
工程的类路径即 src 目录下创建 SpringMVC 的配置文件 springmvc.xml
- "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/context
- https://www.springframework.org/schema/context/spring-context.xsd">
-
-
- <context:component-scan base-package="com.mycompany.controller" />
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/WEB-INF/view/" />
-
- <property name="suffix" value=".jsp" />
- bean>
-
- beans>
8、启动Tomcat,或者未修改Java代码,只修改 jsp 代码,重启资源即可配置如下
选择Edit Configurations...
点击添加,选择Tomcat Server ----> Local
如果这里没有Tomcat Server 选项 ;有可能是tomcat插件没启用,我们点击
file---->setting---->搜索tomcat---->勾选上
(3)配置Tomcat的信息
(4)点击Deployment----加号➕----Artifact...,配置访问路径
对应web.xml文件中的url-pattern 访问的路径
启动Tomcat,输入的访问地址为 网址+端口号+工程名+访问的路径;如:
输入 :http://localhost:8888/spring-web/
@RequestMapping 注解可以定义处理器对于请求的映射规则。该注解可以注解在方法上,也可以注解在类上
1、value 属性
value属性值常以 "/" 开始
@RequestMapping 的 value 属性用于定义所匹配请求的 URI。一个@Controller 所注解的类中,可以定义多个处理器方法。不同的处理器方法所匹配的 URI 是不同的,不同的 URI 被指定在注解于方法之上的@RequestMapping 的value 属性中。但若这些请求具有相同的 URI 部分,则这些相同的 URI,可以被抽取到注解在类之上的@RequestMapping 的 value 属性中。此时的这个 URI 表示模块的名称。URI 的请求是相对于 Web 的根目录
(1)@RequestMapping放在类上面,就是请求地址的公共部分(模块名称)
(2)@RequestMapping放在方法上面,结合类上的RequestMapping的value值即是模块的某个方法请求
2、method属性
Method 属性的取值为 RequestMethod 枚举常量。常用的为 RequestMethod.GET 与RequestMethod.POST,分别表示提交方式的匹配规则为 GET 与 POST 提交
注:
(1)指定了GET请求,就不能使用POST请求,不然报错
(2)不指定请求方式,没有限制,GET和POST皆可
- 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;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- /**
- * @RequestMapping
- * value:放在类上就是请求地址的公共部分(模块名称)
- */
- @Controller
- @RequestMapping("/menu")
- public class MyController {
-
- /**
- * @RequestMapping :请求映射
- * 属性:method,表示请求的方式,值为RequestMethod枚举值
- * Get请求方式,RequestMethod.GET
- * POST请求方式,RequestMethod.POST
- *
- * 指定了请求方式,如Get,使用POST请求会报错:
- * HTTP Status 405 - Request method 'POST' not supported
- */
- //指定some.do使用get请求方式
- @RequestMapping(value = "/some.do",method = RequestMethod.GET)
- public ModelAndView doSome(){ // doGet()----service请求处理
- //处理 some.do请求,相当于service调用处理完成
- ModelAndView mdv = new ModelAndView();
- mdv.addObject("msg","欢迎使用SpringMVC做web开发");
- mdv.addObject("fun","执行的是doSome方法");
- mdv.setViewName("show");
-
- return mdv;
- }
-
- //指定other.do是post请求方式
- @RequestMapping(value = "/other.do",method = RequestMethod.POST)
- public ModelAndView doOther(){
- ModelAndView mv = new ModelAndView();
- mv.addObject("msg","====欢迎使用SpringMVC做web开发====");
- mv.addObject("fun","执行的是doOther方法");
- mv.setViewName("other");
- return mv;
- }
-
- //不指定请求方式,没有限制
- @RequestMapping(value = "/noMethod.do")
- public ModelAndView doNoMethod(HttpServletRequest request,
- HttpServletResponse response,
- HttpSession session){
- ModelAndView mv = new ModelAndView();
- mv.addObject("msg","====欢迎使用SpringMVC做web开发====" + request.getParameter("name"));
- mv.addObject("fun","执行的是doNoMethod方法");
- mv.setViewName("other");
- return mv;
- }
- }
(1)请求参数名与该请求处理方法的参数名相同
index.jsp页面
- <p>提交参数给Controllerp>
- <form action="receiveproperty.do" method="post">
- 姓名:<input type="text" name="name"> <br/>
- 年龄:<input type="text" name="age"> <br/>
- <input type="submit" value="提交参数">
- form>
MyController.java处理器类
- /**
- * 1、逐个接收请求参数
- * 处理器(控制器)方法的形参名和请求中参数名必须一致;同名请求参数赋值给同名形参
- *
- * SpringMVC框架接收请求参数
- * (1)使用request对象接收请求参数
- * String strName = request.getParameter("name");
- * String strAge = request.getParameter("age");
- * (2)SpringMVC框架通过DispatcherServlet调用 MyController的doSome()方法
- * 调用方法时,按名称对应,把接收的参数赋值给形参
- * doSome(strName,Integer.valueOf(strAge))
- * 框架会提供类型转换的功能,能把String转为 int ,long , float, double等类型
- */
- @RequestMapping(value = "/receiveproperty.do")
- public ModelAndView doSome(String name,Integer age){
- System.out.println("doSome, name="+name+" age="+age);
-
- //处理 some.do请求,相当于service调用处理完成
- ModelAndView mdv = new ModelAndView();
- mdv.addObject("myname",name);
- mdv.addObject("myage",Integer.valueOf(age));
- mdv.setViewName("show");
-
- return mdv;
- }
(2)请求参数名与该请求处理方法的参数名不相同
若请求 URL 所携带的参数名称与处理方法中指定的参数名不相同时,则需在处理方法参数前,添加一个注解@RequestParam(“请求参数名”),指定请求 URL 所携带参数的名称
index.jsp页面
- <br/>
- <p>请求参数名和处理器方法的形参名不一样p>
- <form action="receiveparam.do" method="post">
- 姓名:<input type="text" name="rname"> <br/>
- 年龄:<input type="text" name="rage"> <br/>
- <input type="submit" value="提交参数">
- form>
MyController.java处理器类
- /**
- * 逐个接收请求参数
- *
- * 请求中参数名和处理器方法的形参名不一样
- * @RequestParam : 逐个接收请求参数中,解决请求中参数名形参名不一样问题
- * 属性:1、value 请求中的参数名称
- * 2、required boolean类型,默认是true
- * true请求中必须包含此参数
- * 位置:在处理器方法的形参定义前面
- */
- @RequestMapping(value = "/receiveparam.do")
- public ModelAndView receiveParams(@RequestParam(value = "rname",required = false) String name,
- @RequestParam(value = "rage",required = false) Integer age){
- System.out.println("receiveparam, name="+name+" age="+age);
- ModelAndView mv = new ModelAndView();
- mv.addObject("myname",name);
- mv.addObject("m yage",age);
- //show是视图文件的逻辑名称(文件名称)
- mv.setViewName("show");
- return mv;
- }
将处理器方法的参数定义为一个对象,保证请求参数名与这个对象的属性同名即可
- public class Student {
- // 属性名和请求中参数名一样
- private String name;
- private Integer age;
- }
index.jsp页面
- <br/>
- <p>使用java对象接收请求参数p>
- <form action="receiveobject.do" method="post">
- 姓名:<input type="text" name="name"> <br/>
- 年龄:<input type="text" name="age"> <br/>
- <input type="submit" value="提交参数">
- form>
- /**
- * 对象接收请求参数
- */
- @RequestMapping(value = "/receiveobject.do")
- public ModelAndView receiveObject(Student student){
- System.out.println("receiveParam, name=" + student.getName() + " age=" + student.getAge());
- //可以在方法中直接使用 name , age
- //处理some.do请求了。 相当于service调用处理完成了。
- ModelAndView mv = new ModelAndView();
- mv.addObject("myname",student.getName());
- mv.addObject("myage",student.getAge());
- mv.addObject("mystudent",student);
- //show是视图文件的逻辑名称(文件名称)
- mv.setViewName("show");
- return mv;
- }
这里需要注意一个问题,我们使用POST请求,如果参数中包含中文,会出现乱码情况
使用GET请求不会出现此问题,我们需要在 web.xml 配置文件中注册字符集过滤器:spring-web-5.2.5.RELEASE.jar 的org.springframework.web.filter 包下的 CharacterEncodingFilter 类
- public class CharacterEncodingFilter extends OncePerRequestFilter {
-
- protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
- String encoding = this.getEncoding();
- if (encoding != null) {
- if (this.isForceRequestEncoding() || request.getCharacterEncoding() == null) {
- request.setCharacterEncoding(encoding);
- }
-
- if (this.isForceResponseEncoding()) {
- response.setCharacterEncoding(encoding);
- }
- }
-
- filterChain.doFilter(request, response);
- }
- }
- <filter>
- <filter-name>characterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
-
-
- <init-param>
- <param-name>encodingparam-name>
- <param-value>utf-8param-value>
- init-param>
-
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
-
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>characterEncodingFilterfilter-name>
-
- <url-pattern>/*url-pattern>
- filter-mapping>