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
======持续更新中=====
分享到:
相关推荐
<br>=======================<br>步骤:<br>1:配置hibernate,生成po和映射文件 ok<br>2:制作dao,测试 ok <br>3:制作service,测试 ok<br>4:加入Spring IoC,测试 ok<br>5:加入AOP,测试 ok<br>6:加入Struts...
<artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring...
<br><br>支持bean定义配置,web配置,java源代码生成,dao,service,mvc各个层次集成的配置以及相关页面的生产。<br><br>涉及技术:spring mvc,spring ioc+aop,spring dao+jdbc/ibatis/hibernate,log4j,jsp/velocity/...
Struts+Hibernate+Spring+Eclipse已经成为轻量级开发J2EE的标准配置,被称为SHS经典组合,这也是目前Java程序员必须掌握的技能。由于使用范围广也使它们的版本更替非常之快,尤其是Java5推出以后这些项目都采用了...
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 5. **示例应用** - 创建一个简单的 JSF 页面,通过 Spring 注入 Hibernate 服务,执行 CRUD 操作:...
<artifactId>spring-webmvc</artifactId> <version>3.1.0.RELEASE</version> </dependency> ``` **3. Hibernate** Hibernate是Java领域的一个持久化框架,它简化了数据库操作。3.3.1.GA版本支持JPA(Java ...
开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql 内附MySQL表,直接导入就可运行 效果图请移步:http://blog.csdn.net/yangxuan0261/article/details/10053947
<artifactId>spring-webmvc</artifactId> <version>4.3.28.RELEASE</version> </dependency> ``` 接下来,我们需要配置Spring 4。Spring是一个全面的企业级应用开发框架,它支持AOP(面向切面编程)、依赖注入、...
<artifactId>spring-webmvc</artifactId> <version>5.3.18</version> </dependency> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> ...
<spring.version>3.0.5.RELEASE</spring.version> <log4j.version>1.2.16</log4j.version> </properties> ``` - `<spring.version>`:Spring框架的版本号。 - `<log4j.version>`:Log4j的日志库版本号。 #### ...
<artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-...
<artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> ...
<artifactId>spring-webmvc</artifactId> <version>5.x.x.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.x.x.Final...
----linux类系统需要修改mysql的配置文件,改为数据库表名不区分大小写(lower_case_table_names=1) <br /> 环境要求 ------------ 1.jdk要求1.7及以上;<br /> 2.tomcat6或tomcat7; <br /> 3.eclipse版本4.4以上...
<artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</...
1. **配置Spring**:创建Spring的配置文件,配置Bean、事务管理等。 2. **配置SpringMVC**:定义DispatcherServlet,配置视图解析器、拦截器等。 3. **配置MyBatis**:配置SqlSessionFactory,加载Mapper配置文件。 ...
# 基于Spring MVC的简易Web应用 ## 项目概述 本项目是一个基于Spring MVC框架的简易Web应用,旨在展示如何从零开始搭建一个Spring MVC项目,并逐步添加新功能。项目涵盖了基本的Maven配置、Spring配置、web.xml...
<artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> ``` - **spring-web**: 提供了基本的Web应用功能,例如HTTP调用的支持。 - **spring-webmvc**: 提供了Model-View-...
<artifactId>spring-webmvc</artifactId> <version>5.x.x.RELEASE</version> </dependency> <!-- MyBatis 相关依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> ...