SpringMVC Sample Project 2

==================================================
late-nighter
+src
|+com.since2OO7
||+bean
|||+PriceIncreae.java
|||+PriceIncreaeValidator.java
||+bussiness
|||+Product.java
|||+ProductManager.java
||+web
|||+ProductFormController.java
|+firststep
||+LateNighterController.java
|+log4j.properties
|+messages.properties
+jarsystemlib
+tom
+webapplib
|+commons-logging.jar
|+hsqldb.jar
|+jstl.jar
|+log4j-1.2.14,jar
|+spring.jar
|+standard.jar
+build
+WebContent
|+META-INF
|+WEB-INF
||+jsp
|||+hello.jsp
|||+include.jsp
|||+priceincrease.jsp
||+lib
||+tag
|||+spring.tld
||+late-nighter-servlet.xml
||+web.xml
|+index.jsp
//
==================================================
package com.since2OO7.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PriceIncrease {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

private int percentage;

public void setPercentage(int i) {
percentage = i;
logger.info("Percentage set to " + i);
}

public int getPercentage() {
return percentage;
}

}
==================================================
package com.since2OO7.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class PriceIncreaseValidator implements Validator {

private int DEFAULT_MIN_PERCENTAGE = 0;
private int DEFAULT_MAX_PERCENTAGE = 50;
private int minPercentage = DEFAULT_MIN_PERCENTAGE;
private int maxPercentage = DEFAULT_MAX_PERCENTAGE;

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

public boolean supports(Class clazz) {
return clazz.equals(PriceIncrease.class);
}

public void validate(Object obj, Errors errors) {
PriceIncrease pi = (PriceIncrease) obj;
if (pi == null) {
errors.rejectValue("percentage", "error.not-specified", null, "Value required.");
}
else {
logger.info("Validating with " + pi + ": " + pi.getPercentage());
if (pi.getPercentage() > maxPercentage) {
errors.rejectValue("percentage", "error.too-high",
new Object[] {new Integer(maxPercentage)}, "Value too high.");
}
if (pi.getPercentage() <'= minPercentage) {
errors.rejectValue("percentage", "error.too-low",
new Object[] {new Integer(minPercentage)}, "Value too low.");
}
}
}

public void setMinPercentage(int i) {
minPercentage = i;
}

public int getMinPercentage() {
return minPercentage;
}

public void setMaxPercentage(int i) {
maxPercentage = i;
}

public int getMaxPercentage() {
return maxPercentage;
}

}
==================================================
package com.since2OO7.business;

import java.io.Serializable;

public class Product implements Serializable {

private String description;
private Double price;

public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}


}
==================================================
package com.since2OO7.business;

import java.io.Serializable;
import java.util.List;
import java.util.ListIterator;


public class ProductManager implements Serializable {

private List products;

public List getProducts() {
return products;
}

public void setProducts(List products) {
this.products = products;
}

public void increasePrice(int pct) {
ListIterator li = products.listIterator();
while (li.hasNext()) {
Product p = (Product) li.next();
double newPrice = p.getPrice().doubleValue() * (100 + pct)/100;
p.setPrice(new Double(newPrice));
}

}


}
==================================================
package com.since2OO7.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.since2OO7.bean.PriceIncrease;
import com.since2OO7.business.ProductManager;

public class PriceIncreaseFormController extends SimpleFormController {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

private ProductManager productManager;

public ModelAndView onSubmit(Object command)
throws ServletException {

int increase = ((PriceIncrease) command).getPercentage();
logger.info("Increasing prices by " + increase + "%.");

productManager.increasePrice(increase);

String now = (new java.util.Date()).toString();
logger.info("returning from PriceIncreaseForm view to " + getSuccessView());

return new ModelAndView(new RedirectView(getSuccessView()));
}

protected Object formBackingObject(HttpServletRequest request) throws ServletException {

PriceIncrease priceIncrease = new PriceIncrease();
priceIncrease.setPercentage(20);

return priceIncrease;

}

public ProductManager getProductManager() {
return productManager;
}

public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}

}
==================================================
package firststep;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.since2OO7.business.ProductManager;

public class LateNighterController implements Controller {

/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());

private ProductManager productManager = null;

public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {

String now = (new java.util.Date()).toString();
logger.info("returning hello view with " + now);

Map model = new HashMap();
model.put("now", now);
model.put("products", getProductManager().getProducts());

return new ModelAndView("hello", "model", model);

}

public ProductManager getProductManager() {
return productManager;
}

public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}

}
==================================================
# Log4JLogger初期化パラメタ設定
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
# ログレベル、アペンダ名設定
log4j.rootCategory=INFO, consoleLog
#log4j.rootLogger=INFO, stdout, logfile

# ログレベルの詳細設定
log4j.category.jp.co.nttdata=DEBUG
log4j.category.org.springframework=INFO
log4j.category.org.apache.struts=INFO

# コンソールアペンダ設定
log4j.appender.consoleLog=org.apache.log4j.ConsoleAppender
log4j.appender.consoleLog.Target = System.out
log4j.appender.consoleLog.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleLog.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss}][%p][%C{1}] %m%n

# ファイルアペンダ設定
#log4j.appender.fileLog=org.apache.log4j.FileAppender
#log4j.appender.fileLog.File = C:\\tmp\\hoge.log
#log4j.appender.fileLog.layout = org.apache.log4j.PatternLayout
#log4j.appender.fileLog.layout.ConversionPattern=<'%d> %m%n

==================================================
#messages.properties
title=late-nighter
heading=latenighter
greeting=Greetings, it is now

priceincrease.heading=Price Increase :: SpringApp
error.not-specified=Percentage not specified!!!
error.too-low=You have to specify a percentage higher than {0}!
error.too-high=Don't be greedy - you can't raise prices by more than {0}%!
required=Entry required.
typeMismatch=Invalid data.
typeMismatch.percentage=That is not a number!!!

==================================================
<'%@ include file="/WEB-INF/jsp/include.jsp" %>
<'html>
<'head><'title><'fmt:message key="title"/><'/title><'/head>
<'body>
hello.jsp
<'hr>

<'fmt:message key="heading"/>
<'p><'fmt:message key="greeting"/> <'c:out value="${model.now}"/><'/p>

<'br><'br>
Products
<'br><'br>
<'c:forEach items="${model.products}" var="prod">
<'c:out value="${prod.description}"/> <'i>$<'c:out value="${prod.price}"/><'/i>
<'br><'br>
<'/c:forEach>


<'br>
<'a href="<'c:url value="priceincrease.htm"/>">Increase Prices<'/a>
<'br>


<'/body>
<'/html>
<'!--hello.jsp-->
==================================================
<'%@ page session="false"%>

<'%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<'%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<'!--include.jsp-->
==================================================
<'%@ include file="/WEB-INF/jsp/include.jsp" %>
<'%@ taglib prefix="spring" uri="/spring" %>
<'%--
<'%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
write as above, and dont need to use the spring.tld.
--%>
<'html>
<'head><'title><'fmt:message key="title"/><'/title><'/head>
<'body>
<'h1><'fmt:message key="priceincrease.heading"/><'/h1>
<'form method="post">
<'table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
<'tr>
<'td alignment="right" width="20%">Increase (%):<'/td>
<'spring:bind path="priceIncrease.percentage">
<'td width="20%">
<'input type="text" name="percentage" value="<'c:out value="${status.value}"/>">
<'/td>
<'td width="60%">
<'font color="red"><'c:out value="${status.errorMessage}"/><'/font>
<'/td>
<'/spring:bind>
<'/tr>
<'/table>
<'br>
<'spring:hasBindErrors name="priceIncrease">
<'b>Please fix all errors!<'/b>
<'/spring:hasBindErrors>
<'br><'br>
<'input type="submit" alignment="center" value="Execute">
<'/form>
<'a href="<'c:url value="hello.htm"/>">Home<'/a>
<'/body>
<'/html>
<'!--priceincrease.jsp-->
==================================================
<'?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:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd">

<'!--
This is the file where definitions used by the DispatcherServlet should be entered.
-->

<'!-- Application context definition for "late-nighter" DispatcherServlet. -->
<'bean id="lateNighterController" class="firststep.LateNighterController">
<'property name="productManager">
<'ref bean="productManager"/>
<'/property>
<'/bean>

<'!-- Validator and Form Controller for the "Price Increase" page -->
<'bean id="priceIncreaseValidator" class="com.since2OO7.bean.PriceIncreaseValidator"/>

<'bean id="priceIncreaseForm" class="com.since2OO7.web.PriceIncreaseFormController">
<'property name="sessionForm"><'value>true<'/value><'/property>
<'property name="commandName"><'value>priceIncrease<'/value><'/property>
<'property name="commandClass"><'value>com.since2OO7.bean.PriceIncrease<'/value><'/property>
<'property name="validator"><'ref bean="priceIncreaseValidator"/><'/property>
<'property name="formView"><'value>priceincrease<'/value><'/property>
<'property name="successView"><'value>hello.htm<'/value><'/property>
<'property name="productManager">
<'ref bean="productManager"/>
<'/property>
<'/bean>

<'bean id="productManager" class="com.since2OO7.business.ProductManager">
<'property name="products">
<'list>
<'ref bean="product1"/>
<'ref bean="product2"/>
<'/list>
<'/property>
<'/bean>

<'bean id="product1" class="com.since2OO7.business.Product">
<'property name="description"><'value>Lamp<'/value><'/property>
<'property name="price"><'value>5.75<'/value><'/property>
<'/bean>

<'bean id="product2" class="com.since2OO7.business.Product">
<'property name="description"><'value>Table<'/value><'/property>
<'property name="price"><'value>75.25<'/value><'/property>
<'/bean>

<'!-- a url mapping for the DispatcherServlet knowing which controller should be invoked for different url:s. -->
<'bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<'property name="mappings">
<'props>
<'prop key="/hello.htm">lateNighterController<'/prop>
<'prop key="/priceincrease.htm">priceIncreaseForm<'/prop>
<'/props>
<'/property>
<'/bean>

<'bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<'property name="viewClass"><'value>org.springframework.web.servlet.view.JstlView<'/value><'/property>
<'property name="prefix"><'value>/WEB-INF/jsp/<'/value><'/property>
<'property name="suffix"><'value>.jsp<'/value><'/property>
<'/bean>

<'!-- the messageSource entry that will pull in the messages resource bundle ('messages.properties'). -->
<'bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<'property name="basename"><'value>messages<'/value><'/property>
<'/bean>



<'/beans>
<'!--late-nighter-servlet.jsp-->
==================================================
<'?xml version="1.0" encoding="UTF-8"?>
<'web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<'display-name>late-nighter<'/display-name>

<'servlet>
<'servlet-name>late-nighter<'/servlet-name>
<'servlet-class>org.springframework.web.servlet.DispatcherServlet<'/servlet-class>
<'!-- DispatcherServlet that is going to control where all our request are routed. -->
<'load-on-startup>1<'/load-on-startup>
<'/servlet>

<'servlet-mapping>
<'servlet-name>late-nighter<'/servlet-name>
<'url-pattern>*.htm<'/url-pattern>
<'/servlet-mapping>

<'welcome-file-list>
<'welcome-file>index.jsp<'/welcome-file>
<'/welcome-file-list>

<'taglib>
<'taglib-uri>/spring<'/taglib-uri>
<'taglib-location>/WEB-INF/tag/spring.tld<'/taglib-location>
<'/taglib>

<'/web-app>
<'!--web.xml-->

==================================================
<'%@ include file="/WEB-INF/jsp/include.jsp" %>

<'c:redirect url="/hello.htm"/>


<'html>
<'head><'title>index.jsp<'/title><'/head>
<'body>
index.jsp
<'hr>
the entry point for the application.
<'/body>
<'/html>
==================================================
http://localhost:8080/late-nighter
==================================================
2007-07-04 23:08 : __fw__spring : コメント : 0 : トラックバック : 0 :
コメントの投稿
非公開コメント

« next  ホーム  prev »

search

ad



counter


tag cloud

category cloud