如何在JSP Spring MVC中显示类别名称而不是类别ID

时间:2018-10-11 10:01:50

标签: java spring model-view-controller

我有两个类别,分别是产品和类别。在班级产品是从班级类别扩展的,我希望班级产品仅存储categoryId,但在jsp页面上显示类别名称。它没有错误,但是在product.jsp视图中没有显示categoryName。我的机制是:

Product.java

public class Product extends Category{
    private String productId;
    private String productCode;
    private String productName;
    private String description;
    private BigDecimal unitCost;        
    private String warranty;    
    private String manufacturer;
    private String supplier;
    private boolean discontinued;
    private String condition;
    private BigDecimal reOrderLevel;    
    private BigDecimal quantityPerUnit      
    private long unitSold;
    private long unitInStock;
    private long unitInOrder;
    private String categoryId;

    public Product() {
        super();    
    }

    public Product(String productId, String productName, BigDecimal unitCost, String categoryName) {
        super(categoryName);
        // TODO Auto-generated constructor stub
        this.productId = productId;
        this.productName = productName;
        this.unitCost = unitCost;
    }

    Here Getter and Setter Block of Code

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((productId == null) ? 0 
                :productId.hashCode());
        return result;
    }
}

Category.java

public class Category {
    private String categoryId;
    private String categoryName;
    private String categoryType;
    private String categoryStatus;

    public Category(){
        super();
    }

    public Category(String categoryName) {
        this.categoryName = categoryName;
    }

        Here Getter and Setter Block of Code
}

InMemoryProductReposition.java

@Repository
public class InMemoryProductRepository implements ProductRepository {
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

@Override
public List<Product> getAllProducts(){
    /*String SQL = "SELECT * FROM PRODUCTS";*/              
    String SQL = "SELECT PRODUCTS.PRODUCT_ID, PRODUCTS.PRODUCT_CODE, PRODUCTS.PRODUCT_NAME, PRODUCTS.DESCRIPTION, PRODUCTS.UNIT_COST, PRODUCTS.WARRANTY, PRODUCTS.MANUFACTURER, PRODUCTS.SUPPLIER, PRODUCTS.DISCONTINUED, PRODUCTS.CONDITION, PRODUCTS.UNIT_SOLD, PRODUCTS.UNIT_IN_STOCK, PRODUCTS.UNIT_IN_ORDER, PRODUCTS.CATEGORY_ID, CATEGORIES.CATEGORY_NAME FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.CATEGORY_ID = CATEGORIES.CATEGORY_ID";
    Map<String, Object> params = new HashMap<String, Object>();
    List<Product> result = jdbcTemplate.query(SQL,params, new ProductMapper());
    return result;
}

ProductController.java

    @RequestMapping("/product/list")
    public String list(Model model){
        model.addAttribute("product", productService.getAllProducts());
        return "product"; //view name "products.jsp"
    }

product.jsp

<c:forEach items="${product}" var="product">
                <div class="col-sm-6 col-md-3">
                    <div class="thumbnail">
                        <div class="caption">
                            <h3>${product.productName}</h3> <!-- product is on above declaration -->
                            <p>${product.description}</p>                           
                            <p>${product.unitCost}$</p> <!-- one among 2 dolloar sign is currency sign -->
                            <p>${product.warranty}</p>
                            <p>Name ${product.categoryName}</p>
                            <p>Available ${product.unitInStock} units in stock</p>
                            <p>
                                <a href="<spring:url value="/market/product/detail?id=${product.productId}"/>" class="btn btn-primary">
                                    <span class="glyphicon-info-sign glyphicon"></span> Detail                                                  
                                </a>
                            </p>
                        </div>                      
                    </div>
                </div>
            </c:forEach>

1 个答案:

答案 0 :(得分:0)

您应在PRODUCT.CATEGORY_ID中将“ CATEGORIES.CATEGORY_NAME”设置为ProductMapper,并使用<p>Name ${product.categoryId}</p>,这将显示类别名称。