java mvc框架中的m指什么_javaSSM框架之springMVC框架核心总结

项目目录

214642424_1_2021020205110737_wm

/springmvc-servlet.xml核心配置(自定义文件名)

注意:base-package要和controller层保持一致,涵盖处理器映射器,处理器适配器,视图解析器

其余配置亘古不变,请放心使用<?xml  version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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-4.3.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

//固定不变

12345678910111213141516171819202122

web.xml核心配置

org.springframework.web.servlet.DispatcherServlet和

org.springframework.web.filter.CharacterEncodingFilter是

springMVC提供的servlet调度器和编码过滤器

注意的是前者匹配所有请求,后者匹配所有请求和所有jsp页面

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-servlet.xml

1

springmvc

/

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encoding

/*1234567891011121314151617181920212223242526272829303132333435

controller示例

解析:

1.@Controller注解为控制器

2.return "hello"会根据/springmvc-servlet.xml中视图解析器的prefix和suffix来匹配url对应的前端页面,如:url为 ‘http://localhost:8080/hello/h1’则访问项目目录的/WEB-INF/jsp/hello.jsp

214642424_2_20210202051107240_wm

3.Model model;model.addAttribute(“msg”,“12121”);通常用来设置前端数据的回显,下文jackson将详细说明@Controller@RequestMapping("hello")public class HelloController {

@RequestMapping("h1")

public String hello(Model model){

model.addAttribute("msg","12121");

return "hello";

}

@RequestMapping("h2")

public String hello1(Model model){

model.addAttribute("msg1","helloSpringMvc");

return "hello1";

}}123456789101112131415

/hello.jsp文件

${msg} 即对应 model.addAttribute(“msg”,“12121”);

Title${msg}123456789

controller层接受前端参数

前端代码

Title123456789101112

controller层@Controllerpublic class EncodingController {

@PostMapping("/test1")

public String test1(String name, Model model) {

System.out.println(name);

model.addAttribute("msg",name);

return "hello";

}}123456789

解析:

前端输入框的name再没使用jackson时,后台拿到的就是name

@PostMapping("/test1") :由于请求有多种方式,所以RequestMapping就对应有PostMapping/GetMapping/PutMapping等等,只要和前端的method="post"匹配即可,RequestMapping通配

public String test1(String name, Model model) {} 形参String name获取到前端传递的参数

214642424_3_20210202051107302_wm

214642424_4_20210202051107349_wm

214642424_5_20210202051107521_wm

jackson使用

1.补充项目目录

214642424_6_20210202051107568_wm

2.jar包

com.fasterxml.jackson.core

jackson-core

2.10.0

com.fasterxml.jackson.core

jackson-databind

2.10.012345678910

3.工具类JsonUtils.getJson类//抽取出公共的getJson类public class JsonUtils {

public static String getJson(Object object,String dateFormat){

//jackson中有ObjectMapper对象

ObjectMapper mapper = new ObjectMapper();//        不使用时间戳的方式

mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);//        自定义日期的格式

SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

mapper.setDateFormat(sdf);

try {

return mapper.writeValueAsString(object);

} catch (JsonProcessingException e) {

e.printStackTrace();

}

return null;

}}123456789101112131415161718

4.controller类示例@Controllerpublic class JsonController {

@RequestMapping("j3")

@ResponseBody //不会执行任何视图,只返回一个字符串

public String json3() throws JsonProcessingException {

List userList = new ArrayList();

userList.add(new User("黄杰1",20,"男"));

userList.add(new User("黄杰2",20,"男"));

userList.add(new User("黄杰3",20,"男"));

userList.add(new User("黄杰4",20,"男"));

return JsonUtils.getJson(userList,"");

}}12345678910111213

5./springmvc-servlet.xml中配置解决json乱码(都是写死,可以存为模板)

12345678910

6.\效果图

到这里恭喜你,你已经开始进入接口的编写了

214642424_7_20210202051107709_wm

附录:jar包依赖

这些依赖包,基本贯穿所有springMVC全过程,可以存为模板

org.springframework

spring-webmvc

5.1.9.RELEASE

org.jetbrains

annotations

RELEASE

compile

junit

junit

4.11

test

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.2

javax.servlet.jsp.jstl

jstl-api

1.2

taglibs

standard

1.1.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值