springMVC入门配置

DispatcherServlet

web.xml

 

<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

springapp-servlet.xml

 

<?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-2.5.xsd"
>

<!--
the application context definition for the springappDispatcherServlet
-->
<bean id="productManager" class="com.archie.service.SimpleProductManager">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="product1" class="com.archie.domain.Product">
<property name="description" value="Lamp"></property>
<property name="price" value="5.75"></property>
</bean>
<bean id="product2" class="com.archie.domain.Product">
<property name="description" value="Table"></property>
<property name="price" value="75.25"></property>
</bean>
<bean id="product3" class="com.archie.domain.Product">
<property name="description" value="Chair"></property>
<property name="price" value="22.79"></property>
</bean>

<!--
定义信息资源
<fmt:message key="title"/>
......
-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>

<bean name="/hello.htm" class="com.archie.web.HelloController" />
<bean name="/product.htm" class="com.archie.web.InventoryController">
<!-- productManager字段映射id为productManager的bean -->
<property name="productManager" ref="productManager"></property>
</bean>

<!-- 视图解析器 -->
<bean id="viewResolver"
class
="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


</beans>

 

Product.java

 

package com.archie.domain;

import java.io.Serializable;

@SuppressWarnings(
"serial")
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;
}
public String toString(){
StringBuffer buffer
=new StringBuffer();
buffer.append(
"Description:"+description+";");
buffer.append(
"Price:"+price);
return buffer.toString();
}

 

ProductManager.java

 

package com.archie.service;

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

import com.archie.domain.Product;

public interface ProductManager extends Serializable{

public void increasePrice(int percentage);
public List<Product> getProducts();
}
代码
package com.archie.service;

import java.util.List;

import com.archie.domain.Product;

public class SimpleProductManager implements ProductManager{

private static final long serialVersionUID = 4970899362190001718L;

private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void increasePrice(int percentage) {
if(products!=null){
for(Product product:products){
double newPrice=product.getPrice().doubleValue()*(100+percentage)/100;
product.setPrice(newPrice);
}
}
}
public void setProducts(List<Product> products) {
this.products = products;
}


}

 

InventoryController.java

 

package com.archie.web;

import java.util.Date;
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.archie.service.ProductManager;

public class InventoryController implements Controller{

protected final Log logger=LogFactory.getLog(getClass());
private ProductManager productManager;

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String now
=(new Date()).toString();
logger.info(
"returning hello view with"+now);

Map
<String, Object> myModel=new HashMap<String, Object>();
myModel.put(
"now", now);
myModel.put(
"products", this.productManager.getProducts());
return new ModelAndView("product","model",myModel);
}

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

}

 

 


posted on 2010-12-13 18:33  archie2010  阅读(11081)  评论(0编辑  收藏  举报