以下内容翻译自: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

最新文章

  1. 适配ios10(iTunes找不到构建版本)
  2. weblogic忘记登陆密码
  3. Python图片处理
  4. Dual Number
  5. 搭建C语言开发环境
  6. centos6关闭ipv6
  7. android中的DatePicker与TimePicker
  8. 【Java多线程】互斥
  9. android 打包签名
  10. The 2014 ACM-ICPC Asia Mudanjiang Regional
  11. Diary of Codeforces Round #402 (Div. 2)
  12. Chapter 2 User Authentication, Authorization, and Security(6):服务器权限授予粒度
  13. HTTPS 站点的性能优化
  14. [USACO09JAN]Total Flow
  15. mongodb安全权限设定
  16. cordova开发跨平台应用问题随笔记
  17. 【Spark】RDD操作具体解释3——键值型Transformation算子
  18. TensorFlow人脸识别
  19. 配置centos7 网卡
  20. Leetcode 938. Range Sum of BST

热门文章

  1. 关于CSS中float的两点心得以及清除浮动的总结
  2. Django day32 跨域问题,创建vue项目,axios的使用
  3. 面试说熟练掌握各种MQ?那你先看看这道题,面试官必问!
  4. Android布局属性LayoutParams的理解
  5. ACM_栈的压入、弹出序列
  6. C#中的分层开发
  7. [ BZOJ 3445 ] Roadblock
  8. Hive扩展功能(一)--Parquet
  9. 【PL/SQL】触发器示例:记录加薪
  10. mybatis中映射文件和实体类的关联性