Spring3 REST MVC框架,提速你的Web开发
来源:互联网 发布:javascript实战教程 编辑:程序博客网 时间:2024/06/05 11:53
最近在Java web 项目中需要采用非常简单的REST框架,Struts2、webwork、JSF 经过一番比较,最后选择了Spring3,理由只有一个 “简单,好用,并满足需要”。很久以前就Rod Johnson大叔说 Spring3 全面支持REST风格的Web服务,"We're really seeing extensive interest and growth in REST, and it will have comprehensive support for RESTful Web services," said Johnson,今天亲自尝试了一下,真有点相识恨晚的感觉,如果在这次项目运用没有太大的问题,将来在其他项目会大量运用。
工作原理如图所示:
*根据HTTP请求的URL,调用相应的DispatcherServlet控制器。
*提供一个视图是作为HTTP响应发送。
页面上最终运行效果,如图所示:
主要代码:
清单1:TopicController
package com.javabloger.springrest.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
@Controller
@RequestMapping("/topic") //url映射的名称
public class TopicController {
@RequestMapping(value = "/{id}",method=RequestMethod.GET)
public String helloWorld(
@PathVariable Long id,
HttpServletRequest request,
HttpServletResponse response) {
request.setAttribute("message", "You Input Topci Id is: <b>"+id+"</b>");
return "topic" ; // 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件
}
@RequestMapping(value="/add")
public String test(HttpServletRequest request,
HttpServletResponse response){
System.out.println("Hello www.JavaBloger.com ");
request.setAttribute("message", "Hello JavaBloger ! ,@RequestMapping(value='/add')");
return "topic"; // 对应 /WEB-INF/jsp 目录下的 topic.jsp 文件
}
}
清单2 :UserController
package com.javabloger.springrest.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javabloger.springrest.pojo.Users;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value="/login")
public String test(HttpServletRequest request,
HttpServletResponse response,Users userinfo){ // 非常方便可以直接在方法里面放入对象
if (userinfo.getUsername().equals("username") &&
userinfo.getPassword().equals("password")
)
{
request.setAttribute("user", userinfo);
return "users/list"; //判断,将跳转不同的页面
}
else{
return "users/loginerr"; //判断,将跳转不同的页面
}
}
}
清单3:web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
清单4:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!– 自动搜索@Controller标注的类 –>
<context:component-scan base-package="com.javabloger.springrest.action" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!– Default ViewResolver –>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="i18n/messages" />
</beans>
- Spring3 REST MVC框架,提速你的Web开发
- Spring3 REST MVC框架,提速你的Web开发 适应ajax请求
- Spring3文件上传,提速你的Web开发
- Spring3文件上传,提速你的Web开发
- 初试Spring3 MVC REST
- 传统的Java Web MVC框架距离REST有多远
- 传统的Java Web MVC框架距离REST有多远
- 传统的Java Web MVC框架距离REST有多远
- 传统的Java Web MVC框架距离REST有多远
- 搭建spring3 的mvc开发
- 如何更好的设计你的REST API之基于REST架构的Web Service设计及REST框架实现
- Struts_基于MVC的Web开发框架
- backbone 重量级Web的MVC开发框架
- rest web service - jersey . spring3
- Spring3 Web MVC下的数据类型转换
- Spring3 Web MVC下的数据类型转换
- 设计REST风格的Java MVC框架
- 【转】设计 REST 风格的 MVC 框架
- 如何学好C++语言
- android系统开发(四)-触摸屏tslib移植(内核)和原理分析
- 瓷器工艺
- 学asp.net需要看什么书,推荐一下,谢谢!
- android系统开发(五)-tslib移植
- Spring3 REST MVC框架,提速你的Web开发
- 20110330-3.3.2-下标操作不可添加元素
- android系统开发(六)-HAL层开发基础
- ospf sham-link配置—基于CISCO 2811
- TMS320F2812外部接口分析与存储器扩展
- REST软件架构
- CSDN里面.Net码字民工薪水多少啊?
- 【叶子函数分享五十四】汉字转拼音函数
- [EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)