spring mvc: xml练习
xml练习,得到的结果是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<code>200</code>
<message>success</message>
<user>
<name>李四</name>
<id>2</id>
</user>
</xml>
需要用的的注解:@ResponseBody
org.springframework.web.bind.annotation.ResponseBody;
将内容输出为xml,json格式(非html格式)
其他注解:
javax.xml.bind.annotation.XmlElement;
javax.xml.bind.annotation.XmlElements;
javax.xml.bind.annotation.XmlRootElement;
javax.xml.bind.annotation.XmlAccessorType;
javax.xml.bind.annotation.XmlAccessType;
包名:json
user.java
package json; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType; @XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class User { @XmlElement
String name;
@XmlElement
Integer id; public User(String name, Integer id) {
this.name = name;
this.id = id;
} public User() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} }
BaseXmlResult.java根信息
package json;
public class BaseXmlResult {
protected String code;
protected String message;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
UserActiveResult.java User列表信息
package json; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType; @XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.NONE)
public class UserActiveResult<T> extends BaseXmlResult { @XmlElements({
@XmlElement(name="user", type=User.class)
})
private T data; public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
} public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
} }
再来看看UserController.java
package json; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value="/user")
public class UserController { @RequestMapping(value="/list", method=RequestMethod.GET)
@ResponseBody
public UserActiveResult getUser()
{
UserActiveResult<User> list = new UserActiveResult<User>();
list.setCode("200");
list.setMessage("success"); User user = new User();
user.setId(1);
user.setName("张三"); list.setData(user); return list; }
}
spring mvc: xml练习的更多相关文章
- spring mvc: xml生成
spring mvc: xml生成 准备: javax.xml.bind.annotation.XmlElement; javax.xml.bind.annotation.XmlRootElement ...
- Spring MVC Xml视图解析器
XmlViewResolver用于在xml文件中定义的视图bean来解析视图名称.以下示例演示如何在Spring Web MVC框架使用XmlViewResolver. XmlViewResolver ...
- Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因
1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...
- spring Mvc 执行原理 及 xml注解配置说明 (六)
Spring MVC 执行原理 在 Spring Mvc 访问过程里,每个请求都首先经过 许多的过滤器,经 DispatcherServlet 处理; 一个Spring MVC工程里,可以配置多个的 ...
- Spring MVC MultipartFile实现图片上传
<!--Spring MVC xml 中配置 --><!-- defaultEncoding 默认编码;maxUploadSize 限制大小--><!-- 配置Multi ...
- Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC
内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...
- spring mvc中的service和controller中读取不到properties值
根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式
- spring mvc注解版01
spring mvc是基于servlet实现的在spring mvc xml版中已经说过了,注解版相较于xml版更加简洁灵活. web项目的jar包: commons-logging-1.1.3.ja ...
- mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)
由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...
随机推荐
- java-mybaits-00501-案例-映射分析-订单商品数据模型
1.数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键 ...
- 【转载】ARM MMU详解
一.MMU的产生 许多年以前,当人们还在使用DOS或是更古老的操作系统的时候,计算机的内存还非常小,一般都是以K为单位进行计算,相应的,当时的程序规模也不大,所以内存容量虽然小,但还是可以 ...
- SVM支持向量机总结
一.拉格朗日乘子法 一般,在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件.这里我们先介绍拉格朗日乘子法,后面再介绍KKT条件. 比如考虑下面的组合优化的问题, 这是一个带等式约束的优化 ...
- C语言中const和数组
C语言中const的用法 const:在定义变量时,如果使用关键字const,那就表示限制这个变量值不允许被改变. (1) 修饰变量 const离谁越近,谁的值就不能改变. int const ...
- 20165324 Java实验一
20165324 实验一 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 1 ...
- mysql相关配置
http://www.cnblogs.com/cnblogsfans/archive/2009/09/21/1570942.html http://www.jb51.net/article/31902 ...
- Flask之flask-script模块使用
Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...
- hust1010 The Minimum Length
地址:http://acm.hust.edu.cn/problem/show/1010 题目: 1010 - The Minimum Length Time Limit: 1s Memory Limi ...
- ZOJ - 3229 Shoot the Bullet (有源汇点上下界最大流)
题意:要在n天里给m个女生拍照,每个女生有拍照数量的下限Gi,每天有拍照数量的上限Di,每天当中每个人有拍照的上限Lij和Rij.求在满足限制的基础上,所有人最大能拍多少张照片. 分析:抛开限制,显然 ...
- [转]VMware-Transport(VMDB) error -44:Message.The VMware Authorization Service is not running解决方案
转自:http://blog.sina.com.cn/s/blog_70c9c4b40101i01v.html 1.VMware Workstation中新建的虚拟机在开机的时候出现这种错误:Tran ...