`
狂想的旅程
  • 浏览: 16092 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

<Reslet1>:Reslet+Spring,配置简单web访问

阅读更多
Restlet针对每个url指定一个资源。使用spring注解,代码已经跑通,附件项目下载,欢迎留言讨论
注意:web.xml配置component,
<init-param>
      <param-name>org.restlet.component</param-name>
      <param-value>component</param-value>
</init-param>

param-value要和applicationContext.xml中bean的id保持一致
基本结构如图:




下面上代码:
CustomerDAO
 public interface CustomerDAO {  
    public String getCustomerById(String id);  
}  

OrderDao
public interface OrderDao {
	 public abstract String getOrderId(String id);
}

CustomerDAOImpl
@Service
@Scope("prototype")
public class CustomerDAOImpl implements CustomerDAO {
	public String getCustomerById(String id) {
		return "The customer id is " +id;
	}
}

OrderDaoImpl
@Service
@Scope("prototype")
public class OrderDaoImpl implements OrderDao {

	@Override
	public String getOrderId(String id) {
		return "this order id is "+id;
	}
}

CustomerResource
@Controller
@Scope("prototype")
public class CustomerResource extends ServerResource {  
	
	public CustomerResource() {
		// TODO Auto-generated constructor stub
	}
    String customerId = "";  
  
    @Get  
    public Representation getRepresentation() {  
    	customerId = (String) getRequest().getAttributes().get("custId");  
    	String string = customerDAO.getCustomerById(customerId);
    	return new StringRepresentation(string);  
    }  
    
    @Autowired
    private CustomerDAO customerDAO;
 
}  

OrderResource
@Controller
public class OrderResource extends ServerResource{
	    String orderId = "";  

	    @Get  
	    public Representation getRepresentation() {  
	    	orderId = (String) getRequest().getAttributes().get("orderId");  
	    	return new StringRepresentation(orderDao.getOrderId(orderId));  
	    }  
	    
	    @Autowired
	    private OrderDao orderDao;
}

applicationContext.xml
  <beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-lazy-init="true">

	<context:component-scan base-package="com.sioo" />

	<bean id="component" class="org.restlet.ext.spring.SpringComponent">
		<property name="defaultTarget" ref="restRoute" />
	</bean>

	<bean id="restRoute" class="org.restlet.ext.spring.SpringRouter">
		<property name="attachments">
			<map>
				<entry key="/customer/{custId}">
					<bean class="org.restlet.ext.spring.SpringFinder">
						<lookup-method name="create" bean="customerResource" />
					</bean>
				</entry>
				<entry key="/order/{orderId}">
					<bean class="org.restlet.ext.spring.SpringFinder">
						<lookup-method name="create" bean="orderResource" />
					</bean>
				</entry>
			</map>
		</property>
	</bean>
</beans>  

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/applicationContext.xml</param-value>
		</init-param>
	</servlet>

	<!--Spring ApplicationContext load -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Spring,avoid leaking memory -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	
	<!-- restlet servlet -->
	<servlet> 
		<servlet-name>restlet</servlet-name>
		<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
		<init-param>
      <param-name>org.restlet.component</param-name>
      <param-value>component</param-value>
    </init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>restlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	
</web-app>


配置好启动tomcat浏览器输入:
http://localhost:8080/test-restlet/order/1
页面显示:
this order id is 1

http://localhost:8080/test-restlet/customer/1
页面显示:
The customer id is 1

======持续更新中=====
1
1
分享到:
评论

相关推荐

    struts+spring+hibernate实列

    &lt;br&gt;=======================&lt;br&gt;步骤:&lt;br&gt;1:配置hibernate,生成po和映射文件 ok&lt;br&gt;2:制作dao,测试 ok &lt;br&gt;3:制作service,测试 ok&lt;br&gt;4:加入Spring IoC,测试 ok&lt;br&gt;5:加入AOP,测试 ok&lt;br&gt;6:加入Struts...

    java8+tomcat8+struts2.5+spring4.3+hibernate5.2框架搭建详细过程

    &lt;artifactId&gt;spring-web&lt;/artifactId&gt; &lt;version&gt;${spring.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-test&lt;/artifactId&gt; &lt;version&gt;${spring...

    Spring Project Bulder

    &lt;br&gt;&lt;br&gt;支持bean定义配置,web配置,java源代码生成,dao,service,mvc各个层次集成的配置以及相关页面的生产。&lt;br&gt;&lt;br&gt;涉及技术:spring mvc,spring ioc+aop,spring dao+jdbc/ibatis/hibernate,log4j,jsp/velocity/...

    Java Web整合开发完全自学手册:Struts+Hibernate+Spring+Eclipse源码

    Struts+Hibernate+Spring+Eclipse已经成为轻量级开发J2EE的标准配置,被称为SHS经典组合,这也是目前Java程序员必须掌握的技能。由于使用范围广也使它们的版本更替非常之快,尤其是Java5推出以后这些项目都采用了...

    JSF+Spring+Hibernate 配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; ``` 5. **示例应用** - 创建一个简单的 JSF 页面,通过 Spring 注入 Hibernate 服务,执行 CRUD 操作:...

    jersey 1.17+spring 3.1.0.RELEASE+hibernate 3.3.1.GA maven 配置文

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;3.1.0.RELEASE&lt;/version&gt; &lt;/dependency&gt; ``` **3. Hibernate** Hibernate是Java领域的一个持久化框架,它简化了数据库操作。3.3.1.GA版本支持JPA(Java ...

    开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql

    开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql 内附MySQL表,直接导入就可运行 效果图请移步:http://blog.csdn.net/yangxuan0261/article/details/10053947

    springmvc4+spring4+mybatis3+spring-security3的环境搭建

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;4.3.28.RELEASE&lt;/version&gt; &lt;/dependency&gt; ``` 接下来,我们需要配置Spring 4。Spring是一个全面的企业级应用开发框架,它支持AOP(面向切面编程)、依赖注入、...

    搭建SSM(springMVC+spring+mybatis)框架的maven项目

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.3.18&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Spring Core --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-context&lt;/artifactId&gt; ...

    ( maven中整合Spring+hibernate的pom.xml文件的配置.doc )

    &lt;spring.version&gt;3.0.5.RELEASE&lt;/spring.version&gt; &lt;log4j.version&gt;1.2.16&lt;/log4j.version&gt; &lt;/properties&gt; ``` - `&lt;spring.version&gt;`:Spring框架的版本号。 - `&lt;log4j.version&gt;`:Log4j的日志库版本号。 #### ...

    Spring mvc+Security的简单搭建

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;4.0.0.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Spring Security --&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.security&lt;/groupId&gt; &lt;artifactId&gt;spring-...

    springmvc+spring+mybatis集成框架的环境搭建

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;${org.springframework-version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-tx&lt;/artifactId&gt; ...

    Maven下搭建Struts2+spring+hibernate框架

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.x.x.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt; &lt;version&gt;5.x.x.Final...

    基于SpringMVC+Spring+Hibernate+JQuery+Bootstrap框架的OA系统

    ----linux类系统需要修改mysql的配置文件,改为数据库表名不区分大小写(lower_case_table_names=1) &lt;br /&gt; 环境要求 ------------ 1.jdk要求1.7及以上;&lt;br /&gt; 2.tomcat6或tomcat7; &lt;br /&gt; 3.eclipse版本4.4以上...

    技术分享之Web服务后台开发框架升级:从SpringMVC+Hibernate到SpringBoot+Mybatis

    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/dependency&gt; &lt;!-- MyBatis Starter --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt; &lt;artifactId&gt;mybatis-spring-boot-starter&lt;/...

    SpringMVC+Spring+MyBatis整合完整版Web实例(附数据)

    1. **配置Spring**:创建Spring的配置文件,配置Bean、事务管理等。 2. **配置SpringMVC**:定义DispatcherServlet,配置视图解析器、拦截器等。 3. **配置MyBatis**:配置SqlSessionFactory,加载Mapper配置文件。 ...

    基于Spring MVC的简易Web应用.zip

    # 基于Spring MVC的简易Web应用 ## 项目概述 本项目是一个基于Spring MVC框架的简易Web应用,旨在展示如何从零开始搭建一个Spring MVC项目,并逐步添加新功能。项目涵盖了基本的Maven配置、Spring配置、web.xml...

    Spring4 + Hibernate5详细maven构建.docx

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;${spring.version}&lt;/version&gt; &lt;/dependency&gt; ``` - **spring-web**: 提供了基本的Web应用功能,例如HTTP调用的支持。 - **spring-webmvc**: 提供了Model-View-...

    springmvc+spring+mybatis+maven框架搭建ssm

    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt; &lt;version&gt;5.x.x.RELEASE&lt;/version&gt; &lt;/dependency&gt; &lt;!-- MyBatis 相关依赖 --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mybatis&lt;/groupId&gt; &lt;artifactId&gt;mybatis&lt;/artifactId&gt; ...

Global site tag (gtag.js) - Google Analytics