首页 > springmvc将数据传到freemarker出错

springmvc将数据传到freemarker出错

我想将springmvc中的数据传回到freemarker中。我自己新建了一个controler,希望用这个controler将数据传到我指定的freemarker模板路径去。但是在运行的时候,发现数据传不到指定位置去。路径是一定正确的,我怀疑有几个可能,一是没有配置好;二是代码本身的原因。谷歌也找不到好的东西(找不到应该是和我才接触有关系,弄着东西有点赶鸭子上架的感觉),这里贴出controler的代码和模板部分的关键代码。望指教

controler代码:

package net.biz.controller.shop;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.biz.Pageable;
import net.biz.ResourceNotFoundException;
import net.biz.controller.shop.BaseController;
import net.biz.entity.Attribute;
import net.biz.entity.Brand;
import net.biz.entity.ProductCategory;
import net.biz.entity.Promotion;
import net.biz.entity.Tag;
import net.biz.entity.Product.OrderType;
import net.biz.service.BrandService;
import net.biz.service.ProductCategoryService;
import net.biz.service.ProductService;
import net.biz.service.PromotionService;
import net.biz.service.SearchService;
import net.biz.service.TagService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.home766.service.SchemeService;
import com.home766.service.TeHuiManagerService;


@Controller
//@RequestMapping("/terminal")  //修改前
@RequestMapping("/shop")  //当调用shop是就会调用下面的类
public class ShopindexContorler extends BaseController{
    
    @Resource(name="searchServiceImpl")
    private SearchService searchService;    
    
    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;
    @Resource(name = "brandServiceImpl")
    private BrandService brandService;
    @Resource(name = "promotionServiceImpl")
    private PromotionService promotionService;
    @Resource(name = "tagServiceImpl")
    private TagService tagService;
    @Resource(name="schemeServiceImpl")
    private SchemeService schemeService;
    @Resource(name="teHuiManagerServiceImpl")
    private TeHuiManagerService teHuiManagerService;
    
//    @RequestMapping(value="/index")
//    public String index(){
//        return "terminal/index";
//    }
//    
    /**
     * 列表
     */
    long productCategoryId = 82;
    @RequestMapping(value = "/index")
    public String index(@PathVariable Long productCategoryId, Long brandId, Long promotionId, Long[] tagIds, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Integer pageNumber, Integer pageSize, HttpServletRequest request, ModelMap model) {
        ProductCategory productCategory = productCategoryService.find(productCategoryId);
        if (productCategory == null) {
            throw new ResourceNotFoundException();
        }
        Brand brand = brandService.find(brandId);
        Promotion promotion = promotionService.find(promotionId);
        List<Tag> tags = tagService.findList(tagIds);
        Map<Attribute, String> attributeValue = new HashMap<Attribute, String>();
        if (productCategory != null) {
            Set<Attribute> attributes = productCategory.getAttributes();
            for (Attribute attribute : attributes) {
                String value = request.getParameter("attribute_" + attribute.getId());
                if (StringUtils.isNotEmpty(value) && attribute.getOptions().contains(value)) {
                    attributeValue.put(attribute, value);
                }
            }
        }
        Pageable pageable = new Pageable(pageNumber, pageSize);
        model.addAttribute("orderTypes", OrderType.values());
        model.addAttribute("productCategory", productCategory);
        model.addAttribute("brand", brand);
        model.addAttribute("promotion", promotion);
        model.addAttribute("tags", tags);
        model.addAttribute("attributeValue", attributeValue);
        model.addAttribute("startPrice", startPrice);
        model.addAttribute("endPrice", endPrice);
        model.addAttribute("orderType", orderType);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("page", productService.findPage(productCategory, brand, promotion, tags, attributeValue, startPrice, endPrice, true, true, null, false, null, null, orderType, pageable));
//        return "/shop/product/list";
        return "shop/index";
    }

    
    
    /**
     * 搜索
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search(ModelMap model,String keyword,Pageable pageable) {
        model.addAttribute("page", searchService.search(keyword, null,null,null,pageable));
        model.addAttribute("productKeyword", keyword);
        return "/shop/product/list";
    }
}

模板文件关键代码

[#if page.content?has_content]
                    [#list page.content?chunk(5) as row]
                        [#list row as product]
                            <div class="col-sm-12 col-md-12 col-lg-12 classified-goods-list" >
                                <div class="col-sm-2 col-md-2 col-lg-2">
                                        <div class="classified-goods-info">
                                                <!--<img src="/resources/shop/images/11.png">-->
                                                <img src="${base}/upload/image/blank.gif"  data-original="[#if product.image??]${product.image}[#else]${setting.defaultThumbnailProductImage}[/#if]" />
                                                <!--商品的图片-->
                                                <div>
                                                        <p>${abbreviate(product.name, 60)}</p><!--商品的规格信息-->
                                                        <p>已有210人购买</p><!--后台没有将人数传到freemarker-->
                                                        <p>
                                                            ${currency(product.price, true)}
                                                            [#if setting.isShowMarketPrice]
                                                                [#if product.productCategory??]
                                                                    [#if product.productCategory.name=="特惠活动"]
                                                                        <del>${currency(product.marketPrice, true)}</del>
                                                                    [/#if]
                                                                [/#if]
                                                            [/#if]
                                                        </p><!--商品的价格-->
                                                </div>
                                        </div>
                                        <div class="classified-goods-cart">
                                                <p class="text-center">加入购物车</p>
                                        </div>
                                </div>
                        </div>
                        [/#list]
                    [/#list]
                [#else]
                    ${message("shop.product.noListResult")}
                [/#if]

页面的效果如下:


看着好像shop++的东西


试试这样写:

    public ModelAndView handleRequest() throws Exception {  
       ModelAndView mv = new ModelAndView(“shop/index”);   
       mv.addObject("message", "Hello World!");  
       return mv;  
    } 
【热门文章】
【热门文章】