首页 > 其他分享 >springmvc的简单使用(1)

springmvc的简单使用(1)

时间:2022-08-18 13:47:17浏览次数:58  
标签:return String springmvc 简单 视图 使用 println main

一:怎么使用springmvc:

在resource资源文件中新建springmvc核心配置文件其内容如下:

<context:component-scan base-package="com.ztb.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

分别为包扫描和视图解析器

二:在web.xml文件中注册springmvc:

<servlet>
<servlet-name>springmvc</servlet-name>
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

新建jsp页面与controll类:

<a href="${pageContext.request.contextPath}/ztb.do">访问服务器</a>

@Controller
public class ActionController {
@RequestMapping("/ztb")
public String demo(){
System.out.println("服务器。。。");
return "main";
}
}

1DispatchServlet表示前置控制器,是整个springmvc的控制中心,用户发出请求,dispatchservlet接受请求并拦截请求

2.HandlerMapping为处理器映射。DispatchServlet调用,HandleMapping根据请求url查找Handler

3HandlerExecution表示具体的Handler,主要作用是根据url查找控制器

4HandlerExecution将解析后的信息传给DispatchServlet

5HandlerAadpter表示处理器适配器,按照特定的规则去执行Handler,Handler让具体的Controller执行

6Controller将具体的执行信息返回给HandlerAadpter,如ModelAndView

7HandlerAadpter将视图名或模型传递给DispatchServlet

8DispatchServlet调用视图解析器(ViewResolver)来解析HandlerAadpter传递的逻辑视图名

9视图解析器将解析的逻辑视图名传给DispatchServlet

10DispatchServlet根据视图解析器的视图结果,调用具体的视图

11最终展示给用户

 

二:区分post和get请求:

在页面端如下:

 <form action="${pageContext.request.contextPath}/one.do" method="get">
<input type="submit" name="提交">
</form>

后端如下:

@RequestMapping(value = "/one",method = RequestMethod.GET)
public String demo1(){
System.out.println("get方法");
return "main";
}
@RequestMapping(value = "/one",method = RequestMethod.POST)
public String demo2(){
System.out.println("post方法");
return "main";
}

三:五种数据提交的方式:

1:单个提交数据

<form action="${pageContext.request.contextPath}/one.do">
姓名:<input name="myname"><br>
年龄:<input name="myage"><br>
<input type="submit" name="提交">

@RequestMapping("/one")
public String demo1(String myname,int myage) {
System.out.println(myname+myage);
return "main";
}
只要形参的名字与前端名字对应,就一定能取到数据

2:对象封装数据提交:

@RequestMapping("/two")
public String demo1(User user) {
System.out.println(user.getName()+user.getAge());
return "main";
}
<form action="${pageContext.request.contextPath}/two.do">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" name="提交">
</form>
会自动创建对象,要注意前端提交的名称要和实体类中的成员变量的名称一致

3:占位符提交数据:

仅限于超链接或地址栏提交数据,他是一杠一值,一杠一大括号,使用注解来解析

@RequestMapping("/three/{name}/{age}")
public String demo2(
@PathVariable
String name,
@PathVariable
int age) {
System.out.println(name+age);
return "main";
}
<a href="${pageContext.request.contextPath}/three/张三/22.do">动态提交</a>

4:隐射名称不一致:

前端方法的名称和后端的形参名称不一致,使用注解来解析:

@RequestMapping("/four")
public String demo3(
@RequestParam("name")
String uname,
@RequestParam("age")
String ugae) {
System.out.println(ugae+uname);
return "main";
}
<form action="${pageContext.request.contextPath}/four.do">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" name="提交">
</form>

5:手工提取数据:

@RequestMapping("/five")
public String demo3(HttpServletRequest request) {
String name = request.getParameter("name");
int age =Integer.parseInt(request.getParameter("age"));
System.out.println(age+name);
return "main";
}
<form action="${pageContext.request.contextPath}/five.do">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" name="提交">
</form>

标签:return,String,springmvc,简单,视图,使用,println,main
From: https://www.cnblogs.com/zhangtaibing/p/16598400.html

相关文章

  • 荣耀手机如何使用备忘录记重要内容呢?
    无论是在生活还是在工作或学习中,都会有一些比较重要的事情需要我们记住,并且在需要的时候能够很快的想起来。但是有一部分网友表示自己记忆力并不是很好,担心会经常忘记重要......
  • [记]Rust在多线程里使用串口
    1.toml[dependencies]serial="0.4.0"encoding="0.2.33"--usestd::io::{Read,Write};usestd::sync::{Arc,Mutex};usestd::thread;usestd::time::Dura......
  • 【ARK UI】HarmonyOS ETS 资源管理基本使用
    ​代码实现api讲解getStringArraygetStringArray(resId:number,callback:AsyncCallback<Array>):void用户获取指定资源ID对应的字符串数组,使用callback形式返......
  • 迭代器Iterator的使用方法(Java)
    迭代器是一种经典的设计模式。用于在不需要暴漏数据是如何保存在数据结构中的细节的情况下,遍历一个数据结构。Collection接口继承自Iterable接口。所以说,实现了Collectio......
  • 点击行设置样式,在已经使用@row-click的情况下,不同使用其他的事件的时候
    点击行设置样式,在已经使用@row-click的情况下,不同使用其他的事件的时候,出现耦合性的时候,解决方案在<el-table>标签中使用  highlight-current-row实例:1.......
  • 11、函数的基本使用
    11、函数的基本使用  目录:一引入二定义函数三调用函数与函数返回值视频链接 一引入​基于前一部分的学习,我们已经能开发一些功能简单的小......
  • 安装油猴tampermonkey脚本+Greasy Fork及使用教程
    google浏览器安装tampermonkey扩展插件下载地址:链接:https://pan.baidu.com/s/1LBRYHcdkZxfFtBKwMagcmw提取码:nu14安装开启扩展插件:GreasyFork脚本脚本下载......
  • Parallels18永久使用版
    mac软件下载:https://mac.macxf.com/mac/3815.html?id=NjU2MTE%3DParallelsDesktop18简称PD18,一款在Mac上同时运行macOS和Windows,支持Intel和M芯片的虚拟机,在Mac与Win......
  • 关于!this.IsPostBack 使用介绍
    https://blog.csdn.net/panda_xingfu/article/details/9468695如果我们需要某些代码只需要执行一次,最好的选择当然是放在if(!this.IsPostBack){}里面.如果我们不写......
  • jenkins linux服务器使用ssh实现免密登陆
    需求Jenkins部署的时候需要登陆到部署机执行shell脚本,然后采用ssh免密登陆。具体步骤(1)、客户端首先向服务器发送要对其进行身份验证的密钥对的用户名。(2)、服务器检......