spring mvc: xml生成
spring mvc: xml生成
准备:
javax.xml.bind.annotation.XmlElement;
javax.xml.bind.annotation.XmlRootElement;
spring类:
org.springframework.web.bind.annotation.PathVariable;
org.springframework.web.bind.annotation.ResponseBody;
@PathVariable 映射 URL 绑定的占位符
- 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义
- 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
//@PathVariable可以用来映射URL中的占位符到目标方法的参数中
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id)
{
System.out.println("testPathVariable:"+id);
return SUCCESS;
}
@ResponseBody用法
作用:
- 该注解用于将Controller的方法返回的对象,根据HTTP Request Header的
Accept的内容,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
- 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用.
配置返回JSON和XML数据
- 添加
jackson依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
开启
<mvc:annotation-driven />java代码为
@RequestMapping("/testResponseBody")
public @ResponseBody
Person testResponseBody() {
Person p = new Person();
p.setName("xiaohong");
p.setAge(12);
return p;
}
Person类
@XmlRootElement(name = "Person")
public class Person {
private String name;
private int age;
public String getName() { return name; }
@XmlElement
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
@XmlElement
public void setAge(int age) { this.age = age; }
}
- Ajax代码
$.ajax({
url: "testResponseBody",
type: 'GET',
headers: {
Accept: "application/xml",
// Accept:"application/json",
},
success: function(data, textStatus){
console.log(data);
alert(data);
},
error: function (data, textStatus, errorThrown) {
console.log(data);
},
});
分析
如果没有配置
Person类的XML注解,那么只会JSON数据,无论Accept是什么,如果配置了
Person类的xml注解,那么如果Accept含有applicatin/xml, 就会返回xml数据.例如通过浏览器直接访问,浏览器的http request header appect字段一般都为
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8, 故返回XML数据.
改accept: "application/json",即可返回JSON数据.
用此注解或者ResponseEntity等类似类, 会导致response header含有accept-charset这个字段,而这个字段对于响应头是没有用的,以下方法可以关掉
<mvc:annotation-driven>
<mvc:async-support default-timeout="3000"/>
<!-- utf-8编码 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<property name="writeAcceptCharset" value="false"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
开始:
本例有三个配置文件:web.xml, applicationContent.xml, xml2-servlet.xml
访问地址:
http://localhost:8080/gugua3/user/bagayalu
项目: gugua3
包名:xml2
web.xml:
<web-app>
<display-name>Archetype Created Web Application</display-name> <!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 监听转发 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xml2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xml2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
applicationContent.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 -->
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller -->
<context:component-scan base-package="xml2"/> </beans>
xml2-servlet.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
User.java
package xml2; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement
public class User { String name;
Integer id; 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;
} }
UserController.java
package xml2; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable; @Controller
@RequestMapping(value="/user")
public class UserController { @RequestMapping(value="{name}", method=RequestMethod.GET)
public @ResponseBody User getUser(@PathVariable String name)
{
User user = new User();
user.setName(name);
user.setId(10);
return user; }
}
spring mvc: xml生成的更多相关文章
- 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练习
xml练习,得到的结果是: <?xml version="1.0" encoding="UTF-8" standalone="yes" ...
- spring mvc 自动生成代码
generator mybaits 详细配置: 目录结构 执行命令 OK git:https://gitee.com/xxoo0_297/generator.git
- spring Mvc 执行原理 及 xml注解配置说明 (六)
Spring MVC 执行原理 在 Spring Mvc 访问过程里,每个请求都首先经过 许多的过滤器,经 DispatcherServlet 处理; 一个Spring MVC工程里,可以配置多个的 ...
- Spring MVC入门的实例
作为Spring MVC入门,以XML配置的方式为例.首先需要配置Web工程的web.xml文件. 代码清单14-1:web.xml配置Spring MVC <?xml version=&q ...
- 关于Spring MVC的问题
一.SpringMVC的流程是什么? 1. 用户发送请求至前端控制器DispatcherServlet: 2. DispatcherServlet收到请求后,调用HandlerMapping处理器映射 ...
- 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 ...
随机推荐
- 【开发者笔记】归并排序过程呈现之java内置GUI表示
在网上看到一个视频将各种排序用视频表示出来,配上音乐,挺好玩的样子,就算是不会编程的人看到也会觉得很舒服,碰巧我也正在写归并算法,于是就用java的GUI实现一个. 归并排序的时间复杂度是T(n)=O ...
- 模块讲解----time与date time(时间模块)
time和datetime 在python中,通常有一下几种方式来表示时间:1.时间戳:2.格式化时间字符串:3.元祖(struct_time):其中元祖(struct_time分为九个元素) UTC ...
- SCP命令只能单项拷贝,另一个方向“RSA host key for 172.16.103.176 has changed and you have requested strict checki Host key verification failed. lost connection”问题
[dinghuaneng@95 move_data]$ scp * dinghuaneng@172.16.103.176:/home/dinghuaneng@@@@@@@@@@@@@@@@@@@@@@ ...
- Centos上安装python3.5以上版本
一.准备工作: yum install zlib-devel yum install openssl-devel 二.安装python3.5 wget https://www.python.org/f ...
- RBAC权限模型——项目实战
RBAC权限模型——项目实战
- Linux ./configure --prefix 命令是什么意思?
源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(makeinstall). Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./conf ...
- web性能深入探究 eventloop 与浏览器渲染的时序问题 #
https://github.com/jin5354/404forest/issues/61
- iPhone X 游戏闪退:NSUnknownKeyException
目前很多游戏在iPhone X手机 wifi情况下,启动时候闪退,在4G网络时候不闪退. 闪退的log: #0 Thread NSUnknownKeyException [<UIStatusBa ...
- 搭建Firekylin博客
搭建步骤 1).安装 Node.js curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - yum - ...
- MySQL的索引是什么?怎么优化?
索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的.My ...