Commits

Trey Howard committed 33ebca1

Working version on Tomcat 7

Comments (0)

Files changed (14)

 			<version>6.0</version>
 			<scope>provided</scope>
 		</dependency>
+		<dependency>
+			<groupId>javax.servlet</groupId>
+			<artifactId>jstl</artifactId>
+			<version>1.2</version>
+		</dependency>
+
+		<!-- ********** MySQL ********** -->
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+			<version>5.1.25</version>
+			<scope>runtime</scope>
+		</dependency>
 
 		<!-- ********** Logging ********** -->
 		<dependency>
 			<version>${log4j.version}</version>
 		</dependency>
 
+		<!-- ********** Junit ********** -->
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.8.2</version>
+			<scope>test</scope>
+		</dependency>
+
 		<!-- ********** Spring ********** -->
 		<!-- Core utilities used by other modules. Define this if you use Spring 
 			Utility APIs (org.springframework.core.*/org.springframework.util.*) -->

src/main/java/trey/xmless/bootstrap/DatabaseConfig.java

+package trey.xmless.bootstrap;
+
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.core.env.Environment;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+@Configuration
+@PropertySource("env/workstation.properties")
+public class DatabaseConfig {
+
+	@Autowired
+	Environment env;
+
+	@Bean
+	public DataSource dataSource() {
+		DriverManagerDataSource dataSource = new DriverManagerDataSource();
+		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
+		dataSource.setUrl(env.getProperty("db.url"));
+		dataSource.setUsername(env.getProperty("db.username"));
+		dataSource.setPassword(env.getProperty("db.password"));
+		return dataSource;
+	}
+
+}

src/main/java/trey/xmless/bootstrap/RootConfig.java

 
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
 
 @Configuration
-@ComponentScan(basePackages = { "trey.xmless.service", "trey.xmless.repositories.dao" })
+@Import(DatabaseConfig.class)
+@ComponentScan(basePackages = { "trey.xmless.service", "trey.xmless.dao" })
 public class RootConfig {
 
 	// @Bean public SomeClass someClass() { return someInstance; }

src/main/java/trey/xmless/dao/IPlanetDao.java

+package trey.xmless.dao;
+
+import trey.xmless.model.Planet;
+
+public interface IPlanetDao {
+
+	Planet getPlanet(Long planetId);
+
+}

src/main/java/trey/xmless/dao/PlanetDao.java

+package trey.xmless.dao;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.core.support.JdbcDaoSupport;
+import org.springframework.stereotype.Component;
+
+import trey.xmless.model.Planet;
+
+@Component
+public class PlanetDao extends JdbcDaoSupport implements IPlanetDao {
+
+	@Autowired
+	public PlanetDao(DataSource dataSource) {
+		super();
+		setDataSource(dataSource);
+	}
+
+	public Planet getPlanet(Long planetId) {
+		List<Planet> list = getJdbcTemplate().query("select * from planets where planet_id = ?",
+				new Object[] { planetId }, new PlanetRowMapper());
+		return list.isEmpty() ? null : list.get(0);
+	}
+
+	private static class PlanetRowMapper implements RowMapper<Planet> {
+		public Planet mapRow(ResultSet rs, int rowNum) throws SQLException {
+			Planet p = new Planet();
+			p.setId(rs.getLong("planet_id"));
+			p.setName(rs.getString("name"));
+			return p;
+		}
+	}
+
+}

src/main/java/trey/xmless/model/Planet.java

+package trey.xmless.model;
+
+public class Planet {
+
+	private long id;
+	private String name;
+
+	public long getId() {
+		return id;
+	}
+
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+}

src/main/java/trey/xmless/service/IPlanetService.java

+package trey.xmless.service;
+
+public interface IPlanetService {
+
+	String getPlanetName(Long planetId);
+
+}

src/main/java/trey/xmless/service/PlanetService.java

+package trey.xmless.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import trey.xmless.dao.IPlanetDao;
+import trey.xmless.model.Planet;
+
+@Service
+public class PlanetService implements IPlanetService {
+
+	@Autowired
+	private IPlanetDao planetDao;
+
+	public String getPlanetName(Long planetId) {
+		Planet p = planetDao.getPlanet(planetId);
+		return p == null ? null : p.getName();
+	}
+
+}

src/main/java/trey/xmless/web/HomeController.java

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.MessageSource;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.servlet.ModelAndView;
+
+import trey.xmless.service.IPlanetService;
 
 @Controller
 public class HomeController {
 	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
 
 	@Autowired
-	private MessageSource messageSource;
+	private IPlanetService planetService;
 
 	@RequestMapping(value = "/", method = RequestMethod.GET)
-	public String home() {
-		messageSource.getMessage("home.welcome", new Object[0], "defaultMessage", null);
-		logger.info("Welcome home!");
-		return "home";
+	public ModelAndView home(@RequestParam(value = "id", defaultValue = "3") Long planetId) {
+		String planetName = planetService.getPlanetName(planetId);
+		if (logger.isInfoEnabled()) {
+			logger.info("planetId = " + planetId);
+			logger.info("planetName = " + planetName);
+		}
+
+		ModelAndView mav = new ModelAndView();
+		mav.setViewName("home");
+		mav.addObject("planetName", planetName);
+		return mav;
 	}
 
 }

src/main/resources/env/workstation.properties

+db.url=jdbc:mysql://localhost:3306/universe
+db.username=spring
+db.password=spring

src/main/sql/init.sql

+create database universe;
+grant all on universe.* to spring identified by 'spring';
+use universe;
+
+drop table if exists planets;
+create table planets (
+planet_id int(11) primary key,
+name varchar(255) not null
+);
+
+insert into planets (planet_id, name) values (1, 'Mercury');
+insert into planets (planet_id, name) values (2, 'Venus');
+insert into planets (planet_id, name) values (3, 'Earth');
+insert into planets (planet_id, name) values (4, 'Mars');
+insert into planets (planet_id, name) values (5, 'Jupiter');
+insert into planets (planet_id, name) values (6, 'Saturn');
+insert into planets (planet_id, name) values (7, 'Uranus');
+insert into planets (planet_id, name) values (8, 'Neptune');
+
+select * from planets;

src/main/webapp/WEB-INF/messages.properties

-home.title=Hello World
-home.header=Hello World!
-home.welcome=Welcome home!

src/main/webapp/WEB-INF/views/home.jsp

+<%-- <!DOCTYPE html> --%>
+
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+
 <html>
 	<head>
 		<title>Hello World</title>
 	</head>
 	<body>
-		Hello World!
+		Hello ${planetName}!
 	</body>
 </html>

src/test/java/trey/xmless/bootstrap/TestWebAppInitializer.java

+package trey.xmless.bootstrap;
+
+import static junit.framework.Assert.assertFalse;
+
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+import trey.xmless.dao.PlanetDao;
+
+public class TestWebAppInitializer {
+
+	@Test
+	public void test() throws SQLException {
+		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class);
+
+		// verify that variable substitution is working
+		DriverManagerDataSource dataSource = context.getBean(DriverManagerDataSource.class);
+		assertFalse("${db.url}".equals(dataSource.getUrl()));
+
+		PlanetDao dao = context.getBean(PlanetDao.class);
+		assertFalse("${db.url}".equals(((DriverManagerDataSource)dao.getDataSource()).getUrl()));
+	}
+
+}