/* * @RequestMapping: * value:所有请求地址的公共部分,叫做模块名称 * 位置:放在类的上面 * 通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
web.xml:初始化配置
<?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">
<!-- 声明,注册springmvc的核心对象DispatcherServlet-->
<servlet>
<servlet-name>myweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义springmvc读取的配置文件的位置-->
<init-param>
<!--springmvc的配置文件属性-->
<param-name>contextConfigLocation</param-name>
<!-- 指定自定义文件的位置-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 在tomcat启动后,创建Servlet对象
load-on-startup:表示tomcat启动后创建对象的顺序,它的值是正数,数值越小 tomacat创建对象越早
-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 所有的*..do请求交给myweb中央处理器处理-->
<servlet-mapping>
<servlet-name>myweb</servlet-name>
<url-pattern>*.do</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: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.bjpowernode.controller"/>
<!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀:视图文件的路径-->
<property name="prefix" value="/WEB-INF/view/"/>
<!-- 后缀:视图文件的扩展名-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
MyController:控制器
package com.bjpowernode.controller;
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;
/*
* @RequestMapping:
* value:所有请求地址的公共部分,叫做模块名称
* 位置:放在类的上面
* 通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
@Controller
@RequestMapping("/user")
public class MyController {
/*
@RequestMappering:请求映射
属性:method,表示请求的方式,他的值RequestMethod类枚举值
例如 表示Get请求方式,RequestMethod.Get
表示post方式,RequestMethod.POST
如果请求方式不对应,会报错 Http Status 405
*/
// 指定some.do是GET请求方式
//@RequestMapping(value = {"/some.do","/first.do"},method = RequestMethod.GET) //value里面可以写多个名字进行访问
@RequestMapping(value = "/some.do",method = RequestMethod.GET)
public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎使用mvc开发");
mv.addObject("fun","执行的是dosome方法");
mv.setViewName("show");
//返回mv
return mv;
}
//指定other.do是post请求方式
@RequestMapping(value = "/other.do",method = RequestMethod.POST) //value里面可以写多个名字进行访问
public ModelAndView doOther(){
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎========使用mvc开发==============");
mv.addObject("fun","执行的是dosome方法");
mv.setViewName("other");
//返回mv
return mv;
}
//不指定请求方式,没有限制
@RequestMapping(value = "/first.do")
public ModelAndView doFirst(){
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎-------使用mvc开发--------");
mv.addObject("fun","执行的是doFirst方法");
mv.setViewName("other");
//返回mv
return mv;
}
}
index.xml:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 17:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h1>hellow word</h1>
<p><a href="user/some.do">发起some.do的get请求</a></p>
<br>
<form action="user/other.do" method="post">
<input type="submit" value="post请求other.do">
</form>
<hr>
<h1>hellow word</h1>
<p><a href="user/first.do">发起first.do的get请求</a></p>
<br>
<form action="user/first.do" method="post">
<input type="submit" value="post请求other.do">
</form>
</body>
</html>
other.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 19:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h3>/WEB-INF/view/other.jsp,从request作用域中获取数据</h3>
<h3>msg数据:${mgs}</h3>
<h3>fun数据:${fun}</h3>
</body>
</html>
show.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/6/7
Time: 19:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<title>Title</title>
<base href="<%=basePath%>">
</head>
<body>
<h3>show.jsp,从request作用域中获取数据</h3>
<h3>msg数据:${mgs}</h3>
<h3>fun数据:${fun}</h3>
</body>
</html>
点击第一个:
点击第二个:
点击第三个:
点击第四个:
如果指定请求访问方式,超链接发起Get请求方式,让他访问post请求方式,则会报错:
给doFirst加上请求参数:
package com.bjpowernode.controller;
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:所有请求地址的公共部分,叫做模块名称
* 位置:放在类的上面
* 通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
@Controller
@RequestMapping("/user")
public class MyController {
/*
@RequestMappering:请求映射
属性:method,表示请求的方式,他的值RequestMethod类枚举值
例如 表示Get请求方式,RequestMethod.Get
表示post方式,RequestMethod.POST
如果请求方式不对应,会报错 Http Status 405
*/
// 指定some.do是GET请求方式
//@RequestMapping(value = {"/some.do","/first.do"})
@RequestMapping(value ="/some.do",method = RequestMethod.GET) //value里面可以写多个名字进行访问
public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎使用mvc开发");
mv.addObject("fun","执行的是dosome方法");
mv.setViewName("show");
//返回mv
return mv;
}
//指定other.do是post请求方式
@RequestMapping(value = "/other.do",method = RequestMethod.POST) //value里面可以写多个名字进行访问
public ModelAndView doOther(){
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎========使用mvc开发==============");
mv.addObject("fun","执行的是dosome方法");
mv.setViewName("other");
//返回mv
return mv;
}
//不指定请求方式,没有限制
//添加请求参数
@RequestMapping(value = "/first.do")
public ModelAndView doFirst(HttpServletRequest request,
HttpServletResponse response,
HttpSession session){
System.out.println("调用Sevice后:");//这里先不调用,简化处理
ModelAndView mv=new ModelAndView();
//添加数据,框架在请求的最后把数据放入到request作用域。
//request.setAttribute("msg","欢迎使用mvc开发")
mv.addObject("mgs","欢迎-------使用web开发--------"+request.getParameter("name"));
mv.addObject("fun","执行的是doFirst方法");
mv.setViewName("other");
//返回mv
return mv;
}
}
在地址栏输入传递的值:?name=zhangsan,回车