Spring MVC-集成(Integration)-生成XML示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_xml.htm
说明:示例基于Spring MVC 4.1.6。
以下示例说明如何使用Spring Web MVC框架生成XML。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名为TestWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。 |
2 | 在com.tutorialspoint包下创建一个Java类User,UserController。 |
3 | 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。 |
User.java
package com.tutorialspoint; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "user")
public class User {
private String name;
private int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
}
UserController.java
package com.tutorialspoint; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/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(1);
return user;
}
}
TestWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint" />
<mvc:annotation-driven />
</beans>
在这里,我们创建了一个XML映射的POJO用户,并在UserController中,我们返回了用户。Spring自动处理基于RequestMapping的XML转换
完成创建源文件和配置文件后,导出应用程序。右键单击应用程序并使用Export > WAR File选项,并将您的TestWeb.war文件保存在Tomcat的webapps文件夹中。
现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试URL http://localhost:8080/TestWeb/mahesh,您应该看到以下结果。
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test28
Spring MVC-集成(Integration)-生成XML示例(转载实践)的更多相关文章
- Spring MVC使用@RestController生成JSON示例
继上一章的生成JSON示例http://www.cnblogs.com/EasonJim/p/7500405.html,现在还有另一种选择,就是使用@RestController,下面将参照上一节例子 ...
- Spring MVC 使用kaptcha生成验证码
Spring MVC 使用kaptcha生成验证码 1.下载kaptcha-2.3.2.jar(或直接通过该文章附件下载) http://code.google.com/p/kaptcha/downl ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- Spring MVC集成slf4j-logback
转自: Spring MVC集成slf4j-logback 1. Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...
- spring mvc集成freemarker使用
freemarker作为视图技术出现的比velocity早,想当年struts风靡一时,freemarker作为视图层也风光了一把.但现在velocity作为后起之秀的轻量级模板引擎,更容易得到青睐. ...
- spring mvc集成velocity使用
目前流行的三大页面视图神器是:老牌大哥jsp.后起之秀freemarker和velocity.这里不详细比较这三者的优劣,总体来说,jsp是标配,但后面两个更严格的执行了视图与业务的分离,页面里是不允 ...
- spring mvc 集成freemarker模板
主要使用到的jar 文件:spring mvc +freemarker.jar 第一步:spring mvc 集成 freemarker <!-- 定义跳转的文件的前后缀 ,视图模式配置--&g ...
- Spring MVC集成Swagger
什么是Swagger? 大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Servi ...
- Spring MVC集成Log4j
以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...
随机推荐
- 元素类型以及overflow,white-space等属性
1:预格式化标签:<pre></pre>2:overflow属性="visible/hidden(隐藏)"/scroll/auto(自动)/inherit; ...
- 开启和安装Kubernetes 基于Docker For Windows
0.最近发现,Docker For Windows Stable在Enable Kubernetes这个问题上是有Bug的,建议切换到Edge版本,并且采用下文AliyunContainerServi ...
- JavaScript--编程
第一步:把注释语句注释. 第二步:编写代码,在页面中显示 “系好安全带,准备启航--目标JS”文字: 第三步:编写代码,在页面中弹出提示框“准备好了,起航吧!” 提示: 可以把弹框方法写在函数里. 第 ...
- JavaScript入门二
******函数****** **函数定义** //普通函数定义 function f1() { console.log("Hello word!") } //带参数的函数 fun ...
- Codeforces Round #419
A Karen and Morning 找最近的回文时间 模拟 往后推 判判就行 //By SiriusRen #include <bits/stdc++.h> using namesp ...
- Ubuntu下搭建repo服务器(二): 配置git-daemon-run
git-daemon-run实际是一个脚本管理工具,用来启动git-daemon. 1 安装git-daemon-run(A端) apt-get install git-daemon-run 2. 配 ...
- 涨知识---V
1.内存对齐 在解释内存对齐的作用前,先来看下内存对齐的规则: 1). 对于结构的各个成员,第一个成员位于偏移为0的位置,以后每个数据成员的偏移量必须是min(#pragma pack()指定的数, ...
- Elasticsearch之CURL命令的version控制
普通关系型数据库使用的是(悲观并发控制(PCC)) 当我们在修改一个数据前先锁定这一行,然后确保只有读取到数据的这个线程可以修改这一行数据 ES使用的是(乐观并发控制(OCC)) ES不会阻止某一数据 ...
- java学习笔记_BeatBox(GUI部分)
import java.awt.*; import javax.swing.*; public class BeatBox { JFrame theFrame; JPanel mainPanel; S ...
- Android文件操作报open failed: EBUSY (Device or resource busy)
Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...