SpringMVC Sample Project

ref:
http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html
==================================================
late-nighter
+src
|+com.since2OO7.bussiness
||+Product.java
||+ProductManager.java
|+firststep
||+LateNighterController.java
|+log4j.properties
|+messages.properties
+jarsystemlib
+tom
+webapplib
|+commons-logging.jar
|+jstl.jar
|+log4j-1.2.14,jar
|+spring.jar
|+standard.jar
+build
+WebContent
|+META-INF
|+WEB-INF
||+jsp
|||+hello.jsp
|||+include.jsp
||+lib
||+late-nighter-servlet.xml
||+web.xml
|+index.jsp
//
==================================================
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;

public class ProductManager implements Serializable {

private List products;

public List getProducts() {
return products;
}

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


}
==================================================
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;
}

}
==================================================
#[[log4j.properties]]
# 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.firststep=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
==================================================
<'%@ 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>

<'/body>
<'/html>
==================================================
<'%@ 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-->

==================================================
<'?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>

<'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>
<'/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>
<'!--latenighter-servlet.xml-->

==================================================
<'?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>

<'/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
==================================================

ref:
http://hitachisoft.jp/products/research/techdoc/WebApp/SpringWeb/index.html

tag : SpringMVC first step sample

2007-07-01 05:16 : __fw__spring : コメント : 0 : トラックバック : 0 :
コメントの投稿
非公開コメント

« next  ホーム  prev »

search

ad



counter


tag cloud

category cloud