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

 

 

 

 

 

 

 

相关内容

热门资讯

德适暗盘大涨101%:“德智米... (来源:大鱼财经工作室)2026年3月末的港股市场,注定是波涛汹涌的。各大科技龙头企业陆续迎来业绩披...
高效絮凝剂商品报价动态(202... 生意社03月29日讯 交易商品牌/产地交货地最新报价高效絮凝剂 复合絮凝剂巩义市恒毅水...
北京两大新盘首开战绩:国贤府P... 北京进深 赵盼盼 3月29日,北京两大新盘——中建智地国贤府PARK与未来城·星寰时代正式开盘。据官...
比亚迪将于4月1日举行2025... 中访网数据  比亚迪股份有限公司(证券代码:002594)宣布,将于2026年4月1日下午15:00...
法庭文件曝光:马斯克曾邀请扎克... 3月29日消息,据Business Insider报道,根据最新公布的法庭文件,在特斯拉 CEO 埃...