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进行测试  

 

 

 

 

 

 

 

相关内容

热门资讯

法拉电子股价涨3.12%,工银... 6月18日,法拉电子涨3.12%,截至发稿,报175.36元/股,成交6.31亿元,换手率1.63%...
6月17日中证上海国企ETF(... 数据显示,6月17日,中证上海国企ETF(510810)获净申购108.17万元,位居当日股票ETF...
神火股份跌1.02%,成交额4... 6月18日,神火股份盘中下跌1.02%,截至10:17,报24.30元/股,成交4.15亿元,换手率...
汉宇集团涨1.08%,成交额2... 6月18日,汉宇集团盘中上涨1.08%,截至10:17,报10.25元/股,成交2910.82万元,...
6月17日广发中证全指信息技术... 数据显示,6月17日,广发中证全指信息技术ETF(159939)遭净赎回1499.46万元,位居当日...