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

 

 

 

 

 

 

 

相关内容

热门资讯

粤港澳大湾区ETF净值下跌3.... 平安中证粤港澳大湾区发展主题交易型开放式指数证券投资基金(简称:粤港澳大湾区ETF,代码512970...
华夏中证5G通信主题ETF净值... 华夏中证5G通信主题交易型开放式指数证券投资基金(简称:华夏中证5G通信主题ETF,代码515050...
前海开源中证500等权ETF净... 前海开源中证500等权重交易型开放式指数证券投资基金(简称:前海开源中证500等权ETF,代码515...
国泰中证细分化工产业主题ETF... 国泰中证细分化工产业主题交易型开放式指数证券投资基金(简称:国泰中证细分化工产业主题ETF,代码51...
易方达中证智能电动汽车ETF净... 易方达中证智能电动汽车交易型开放式指数证券投资基金(简称:易方达中证智能电动汽车ETF,代码5165...