work on rest controller and separated application contexts

This commit is contained in:
Lucas Pleß 2012-03-07 19:21:53 +01:00
parent 019c235ea5
commit dd4f730ca7
11 changed files with 185 additions and 62 deletions

42
pom.xml
View File

@ -69,12 +69,24 @@
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
</dependencies>
<build>
<plugins>
@ -89,33 +101,9 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<!--
<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>
<configuration>
<glassfishDirectory>/opt/glassfish3/glassfish/</glassfishDirectory>
<user>admin</user>
<adminPassword>foobar2342</adminPassword>
<domain>
<name>${project.artifactId}</name>
<adminPort>4848</adminPort>
<httpPort>8080</httpPort>
<httpsPort>8443</httpsPort>
</domain>
<components>
<component>
<name>${project.artifactId}</name>
<artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
</component>
</components>
</configuration>
</plugin>-->
</plugins>
</build>

View File

@ -1,23 +1,23 @@
package de.ctdo.bunti.web;
import javax.validation.Valid;
import de.ctdo.bunti.control.BuntiController;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.webmodel.DeviceUpdate;
import org.springframework.beans.factory.annotation.Autowired;
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.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class RestController {
private BuntiDevicesDAO devicesDAO;
private Validator validator;
private BuntiController controller;
@Autowired
public final void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
public void setValidator(Validator validator) {
this.validator = validator;
}
@Autowired
@ -25,20 +25,25 @@ public class RestController {
this.controller = controller;
}
@RequestMapping(value = "/devices", method = RequestMethod.GET)
public final ModelAndView getAll() {
return new ModelAndView("deviceList", "deviceList", devicesDAO.getAllDevices());
return new ModelAndView("deviceList", "deviceList", controller.getAllDevices());
}
@RequestMapping(value = "/devices/{id}", method = RequestMethod.GET)
public final ModelAndView getDeviceById(@PathVariable("id") int id) {
return new ModelAndView("device", "device", devicesDAO.getDeviceById(id));
return new ModelAndView("device", "device", controller.getDeviceById(id));
}
@RequestMapping(value = "/devices/{id}", method = RequestMethod.PUT)
public final ModelAndView setDeviceById(@PathVariable("id") int id) {
//controller.setDevice(id, ... )
return new ModelAndView("device", "device", devicesDAO.getDeviceById(id));
public String setDeviceById(@ModelAttribute @Valid DeviceUpdate update, BindingResult errors) {
if (errors.hasErrors()) {
return "redirect:/index.html";
} else {
controller.setDevice(update.getDeviceId(), update.getOptions());
return "redirect:devices/" + update.getDeviceId();
}
}
}

View File

@ -0,0 +1,25 @@
package de.ctdo.bunti.webmodel;
import java.io.Serializable;
import java.util.Map;
public class DeviceUpdate implements Serializable {
private int deviceId;
private Map<String, Object> options;
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public Map<String, Object> getOptions() {
return options;
}
public void setOptions(Map<String, Object> options) {
this.options = options;
}
}

View File

@ -0,0 +1,29 @@
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
>
<context:component-scan base-package="de.ctdo">
<context:exclude-filter type="regex" expression="de\.ctdo\.bunti\.web.*" />
</context:component-scan>
<task:annotation-driven scheduler="myScheduler"/>
<task:scheduler id="myScheduler" pool-size="3"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:bunti.properties"/>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean class="de.ctdo.bunti.dmx.DMXMixerImpl">
<property name="artNetDeviceAddress" value="${artnet.deviceAddress}"/>
</bean>
</beans>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="marshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<map>
<entry value="de.ctdo.bunti.model.BuntiDevice" key="buntiDevice" />
<entry value="de.ctdo.bunti.model.BuntiDMXDevice" key="buntiDMXDevice" />
<entry value="de.ctdo.bunti.model.Par56Spot" key="par56Spot" />
<entry value="de.ctdo.bunti.model.Strobe1500" key="strobe1500" />
</map>
</property>
</bean>
</beans>

View File

@ -1,32 +1,32 @@
<?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:task="http://www.springframework.org/schema/task"
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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="de.ctdo" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:bunti.properties"/>
</bean>
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<task:annotation-driven scheduler="myScheduler"/>
<!--<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="3-10" queue-capacity="50" />-->
<task:scheduler id="myScheduler" pool-size="3"/>
<bean class="de.ctdo.bunti.dmx.DMXMixerImpl">
<property name="artNetDeviceAddress" value="${artnet.deviceAddress}"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
</bean>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2" />
</bean>
<bean id="deviceList" class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller" ref="marshaller" />
<property name="modelKey" value="deviceList" />
</bean>
<import resource="classpath:marshaller.xml" />
</beans>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Device</title>
</head>
<body>
<h2>Device</h2>
<spring:bind path="device">
<font color="red"> <b><c:out value="${status.errorMessage}" /></b></font>
</spring:bind>
<c:out value="${device.deviceId}" />
<br />
<c:out value="${device.deviceName}" />
<br />
<a href="<spring:url value="/"/>">Home</a>
</body>
</html>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Devices</title>
</head>
<body>
<h2>Devices</h2>
<table border="1">
<thead>
<th>Id</th>
<th>Name</th>
</thead>
<c:forEach var="device" items="${deviceList}">
<tr>
<td><c:out value="${device.deviceId}" /></td>
<td><c:out value="${device.deviceName}" /></td>
</tr>
</c:forEach>
</table>
<a href="<spring:url value="/"/>">Home</a>
</body>
</html>

View File

@ -1,7 +1,6 @@
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
@ -12,6 +11,15 @@
<description>CTDO bunti control Server</description>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>