SpringMVC初次尝试
创始人
2024-02-13 13:43:34

1.1、在pom.xml中添加依赖

org.springframeworkspring-context4.2.6.RELEASEorg.springframeworkspring-web4.2.6.RELEASEorg.springframeworkspring-webmvc4.2.6.RELEASE

1.1、添加Spring的配置文件beans.xml



1.3、在web.xml中配置SpringMVC

springMVCorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:beans.xml1springMVC/

1.4、在web.xml中配置允许访问静态文件

default*.jsdefault*.cssdefault*.htmldefault*.gif

1.5、在webapp文件夹下添加demo01.html

在\webapp\views\文件夹下创建demo01.html文件

你好SpringMVC

1.6、添加Controller

package demo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class DemoController {@RequestMapping("/index")public String index1(){return "demo01";}
}

1.7、浏览器访问测试

在浏览器中访问controller,可以正常跳转到demo01.html

二、添加JSON支持

2.1、在beans.xml中添加JSON的消息转换器

2.2、添加User实体

public class User {private int id;private String uname;//省略Getter和Setter
}

2.3、在Controller中测试

package demo.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@RequestMapping("/test2")public User test2(){User user = new User(1001, "张三");return user;}
}

2.4、在浏览器中进行测试

浏览器中访问controller进行测试  

 

 

 

 

 

 

 

相关内容

热门资讯

围绕“提升入境游便利化国际化水... 转自:全国政协5月19日至22日,全国政协副主席、农工党中央常务副主席杨震率全国政协调研组赴重庆市,...
江苏省电子商务职业教育座谈会暨... 2026年5月22日,江苏省电子商务职业教育座谈会暨电子商务服务实体经济研讨会在南京钟山宾馆(江苏省...
本周(5.16-5.22),武... 召开防汛救灾调度会议,开展树立和践行正确政绩观学习教育专题研讨,召开全市领导干部警示教育会,召开市委...
患者福音!无锡这家大医院发力细... 细胞治疗是精准医疗的前沿领域正在为诸多疑难杂症开辟新的治疗路径记者了解到由江南大学附属医院牵头推进的...
猜对有奖!“民法典宣传月”谐音... “民法典宣传月”谐音梗看图猜词特别版重磅上线!没有复杂的法条,只有极简的线条!一张画对应一个民法典词...