mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-02-13 17:41:11 +00:00
petclinic 3 initial commit - wip
This commit is contained in:
parent
349fdef18b
commit
f09d67cc1c
32 changed files with 720 additions and 0 deletions
|
|
@ -0,0 +1,14 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
@RequestMapping(value="/", method = RequestMethod.GET)
|
||||
public void getHome() {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
|
||||
public class Owner {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String address;
|
||||
|
||||
private String city;
|
||||
|
||||
private String telephone;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.util.ResponseContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value="/owners/{owner}")
|
||||
public class OwnerController {
|
||||
|
||||
private final OwnerRepository repository;
|
||||
|
||||
@Autowired
|
||||
public OwnerController(OwnerRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@RequestMapping(method=RequestMethod.GET)
|
||||
public Owner get(Long owner) {
|
||||
return repository.getOwner(owner);
|
||||
}
|
||||
|
||||
@RequestMapping(value="/edit", method=RequestMethod.GET)
|
||||
public Owner getEditForm(Long owner) {
|
||||
return repository.getOwner(owner);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.PUT)
|
||||
public void put(Owner owner, ResponseContext response) {
|
||||
repository.saveOwner(owner);
|
||||
response.redirect(owner.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface OwnerRepository {
|
||||
|
||||
Collection<Owner> findOwnersByLastName(String lastName);
|
||||
|
||||
Owner getOwner(Long id);
|
||||
|
||||
void saveOwner(Owner owner);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.util.ResponseContext;
|
||||
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;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/owners")
|
||||
public class OwnersController {
|
||||
|
||||
private final OwnerRepository repository;
|
||||
|
||||
@Autowired
|
||||
public OwnersController(OwnerRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
public Collection<Owner> get(@RequestParam String lastName) {
|
||||
return repository.findOwnersByLastName(lastName);
|
||||
}
|
||||
|
||||
@RequestMapping(value="/new", method = RequestMethod.GET)
|
||||
public Owner getNewForm() {
|
||||
return new Owner();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public void post(Owner owner, ResponseContext response) {
|
||||
repository.saveOwner(owner);
|
||||
response.redirect(owner.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package org.springframework.samples.petclinic.pet;
|
||||
|
||||
public enum Gender {
|
||||
MALE, FEMALE
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package org.springframework.samples.petclinic.pet;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.samples.petclinic.util.Measurement;
|
||||
|
||||
public class Pet {
|
||||
|
||||
private String name;
|
||||
|
||||
private String species;
|
||||
|
||||
private String breed;
|
||||
|
||||
private Gender gender;
|
||||
|
||||
private Date birthDate;
|
||||
|
||||
private Measurement weight;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package org.springframework.samples.petclinic.pet;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.util.ResponseContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value="/owners/{owner}/pets/{pet}")
|
||||
public class PetController {
|
||||
|
||||
private final PetRepository repository;
|
||||
|
||||
@Autowired
|
||||
public PetController(PetRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@RequestMapping(method=RequestMethod.GET)
|
||||
public Pet get(Long owner, String pet) {
|
||||
return repository.getPet(owner, pet);
|
||||
}
|
||||
|
||||
@RequestMapping(value="/edit", method=RequestMethod.GET)
|
||||
public Pet getEditForm(Long owner, String pet) {
|
||||
return repository.getPet(owner, pet);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.PUT)
|
||||
public void put(Pet pet, ResponseContext response) {
|
||||
repository.savePet(pet);
|
||||
response.redirect(pet.getName());
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.DELETE)
|
||||
public void delete(Long owner, String pet, ResponseContext context) {
|
||||
context.forResource("owners").redirect(owner);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.springframework.samples.petclinic.pet;
|
||||
|
||||
public interface PetRepository {
|
||||
|
||||
Pet getPet(Long owner, String name);
|
||||
|
||||
void savePet(Pet pet);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package org.springframework.samples.petclinic.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class Measurement {
|
||||
|
||||
private BigDecimal amount;
|
||||
|
||||
private Unit unit;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.springframework.samples.petclinic.util;
|
||||
|
||||
public interface ResponseContext {
|
||||
|
||||
void redirect(Object resource);
|
||||
|
||||
ResponseContext forResource(Object resource);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package org.springframework.samples.petclinic.util;
|
||||
|
||||
public enum Unit {
|
||||
POUNDS
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">
|
||||
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
|
||||
</layout>
|
||||
</appender>>
|
||||
|
||||
<!-- Application Loggers -->
|
||||
<logger name="org.springframework.samples">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<!-- 3rdparty Loggers -->
|
||||
<logger name="org.springframework.core">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.beans">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.context">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.web">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn" />
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<%@ page session="false" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Welcome</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
Congratulations! You're running Spring!
|
||||
</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?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"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
|
||||
<!-- Scans within the base package of the application for @Components to configure as beans -->
|
||||
<context:component-scan base-package="org.springframework.samples.petclinic" />
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?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"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
|
||||
<!-- HANDLER MAPPING RULES -->
|
||||
|
||||
<!-- Maps requests to @Controllers based on @RequestMapping("path") annotation values
|
||||
If no annotation-based path mapping is found, Spring MVC proceeds to the next HandlerMapping (order=2 below). -->
|
||||
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
|
||||
<property name="order" value="1" />
|
||||
</bean>
|
||||
|
||||
<!-- Maps requests to @Controllers based on controller class name convention; e.g. a request for /hotels or a /hotels sub-resource maps to HotelsController
|
||||
If no class mapping is found, Spring MVC sends a 404 response and logs a pageNotFound warning. -->
|
||||
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
|
||||
<property name="order" value="2" />
|
||||
</bean>
|
||||
|
||||
<!-- REGISTERED HANDLER TYPES -->
|
||||
|
||||
<!-- Enables annotated @Controllers; responsible for invoking an annotated POJO @Controller when one is mapped. -->
|
||||
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
|
||||
|
||||
<!-- VIEW RESOLUTION AND RENDERING -->
|
||||
|
||||
<!-- Resolves view names to protected .jsp resources within the /WEB-INF directory -->
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
|
||||
<urlrewrite default-match-type="wildcard">
|
||||
<rule>
|
||||
<from>/**</from>
|
||||
<to>/app/$1</to>
|
||||
</rule>
|
||||
<outbound-rule>
|
||||
<from>/app/**</from>
|
||||
<to>/$1</to>
|
||||
</outbound-rule>
|
||||
</urlrewrite>
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
|
||||
|
||||
<!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
|
||||
<filter>
|
||||
<filter-name>UrlRewriteFilter</filter-name>
|
||||
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
|
||||
</filter>
|
||||
|
||||
<filter-mapping>
|
||||
<filter-name>UrlRewriteFilter</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<!-- Handles all requests into the application -->
|
||||
<servlet>
|
||||
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>
|
||||
/WEB-INF/spring/*.xml
|
||||
</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<!-- Maps all /app requests to the DispatcherServlet for handling -->
|
||||
<servlet-mapping>
|
||||
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
|
||||
<url-pattern>/app/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
</web-app>
|
||||
Loading…
Add table
Add a link
Reference in a new issue