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 ...
随机推荐
- R中的一些基础1106
1.R中NA,NaN,Inf代表什么? NA:缺失数据 NaN:无意义的数,比如sqrt(-2) Inf:正无穷大 -Inf:负无穷大 2.确定一个数值型vector的第一个最值(最大/最小)的下标: ...
- sublime2常用插件
Package Control 1. 安装方法 • 下载地址:https://sublime.wbond.net/Package%20Control.sublime-package • 将下载下来的P ...
- windows计划任务定时运行synctoy的坑
每次设置好synctoy之后,需要让synctoy运行一次,windows的计划任务才能成功执行,如果变更了synctoy的设置,而没有让synctoy成功执行过,windows计划任务将执行失败,坑 ...
- bootstrap模态框嵌套、tabindex属性、去除阴影
模态框嵌套 在开发中,遇到需要通过点击事件触发第一个模态框,触发后通过事件唤起第二个模态框,并且通过事件触发第三个模态框:即模态框嵌套. 模态框嵌套需要用一个模态框包裹所涉及嵌套的模态框,从而点击触发 ...
- Eclipse下创建Maven项目(转)
原文出自:http://www.cnblogs.com/hongwz/p/5456616.html 1.新建Maven项目 1.1 File -> New -> Other 1.2 选择M ...
- 对称加密与非对称加密,以及RSA的原理
一 , 概述 在现代密码学诞生以前,就已经有很多的加密方法了.例如,最古老的斯巴达加密棒,广泛应用于公元前7世纪的古希腊.16世纪意大利数学家卡尔达诺发明的栅格密码,基于单表代换的凯撒密码.猪圈密码, ...
- Python3.x:代理ip刷评分
Python3.x:代理ip刷评分 声明:仅供为学习材料,不允许用作商业用途: 一,功能: 针对某网站对企业自动刷评分: 网站:https://best.zhaopin.com/ 二,步骤: 1,获取 ...
- MVC通过服务端对数据进行验证(和AJAX验证一样)
在实体类中 添加 Remote属性,指定用某个View下的某个方法进行验证,如下面表示用User控制器中的UserExiting方法验证 public class User { [Remot ...
- 实现在vista和win7中使用管理员权限接收WM_DROPFILES(OnDropFiles())消息的方法(好像XP不支持这个函数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #pragma once #ifndef WM_COPYGLOBALD ...
- backstopJS 参数详解 CN
最近需要进行前端UI验证,详细研究了下backstopJS.官方文档为En,手动翻译了下.相关参数信息如下: 如需转载引用,请保留原文出处,支持原创,感谢.