<Reslet1>:Reslet和Spring配置简单web访问_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > <Reslet1>:Reslet和Spring配置简单web访问

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

 2015/4/15 15:23:48  狂想的旅程  程序员俱乐部  我要评论(0)
  • 摘要:Restlet针对每个uri指定一个资源。使用spring注解,代码已经跑通,附件项目下载,欢迎留言讨论注意:web.xml配置component,<init-param><param-name>org.restlet.component</param-name><param-value>component</param-value></init-param>param-value要和applicationContext
  • 标签:Web 配置 Spring
Restlet针对每个uri指定一个资源。使用spring注解,代码已经跑通,附件项目下载,欢迎留言讨论
注意:web.xml配置component,
class="java">
<init-param>
      <param-name>org.restlet.component</param-name>
      <param-value>component</param-value>
</init-param>

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


DBAA90F1A34.jpg" />

下面上代码:
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

======持续更新中=====
  • 大小: 31.6 KB
  • test-restlet.7z (5.2 MB)
  • 下载次数: 0
  • 查看图片附件
发表评论
用户名: 匿名