Spring框架 - 数据不在视图页面中显示

时间:2017-07-14 04:26:55

标签: spring hibernate spring-mvc web

我的视图中没有显示任何错误...但是当它运行时,它会将值设为' 0'。我的数据库表名是' categories' .it的值类似于' category_l dummy'对于列。但在视图中显示为0 ...请帮我解决这个问题......

这是我的模特课

@Entity
@Table(name = "categories")
public class CategoriesModel implements Serializable{

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO) //for autonumber
    private int id;
    @Column
    private String category1;
    @Column
    private String desccategory1;

    public CategoriesModel() {
    }

    public CategoriesModel(
            int id,
            String category1, String desccategory1) {
        super();
        this.id = id;
        this.category1 = category1;
        this.desccategory1 = desccategory1;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCategory1() {
        return category1;
    }

    public void setCategory1(String category1) {
        this.category1 = category1;
    }

    public String getDesccategory1() {
        return desccategory1;
    }

    public void setDesccategory1(String desccategory1) {
        this.desccategory1 = desccategory1;
    }

这是我的Dao课程

public interface CategoriesDao {

    public void add(CategoriesModel categories);
    public void edit(CategoriesModel categories);
    public void delete(int id);

    public CategoriesModel getCategoriesModel(int id);

    public List getAllCategoriesModel();
}

这是我的Dao impl课程

@Repository
public class CategoriesDaoImpl implements CategoriesDao {

    @Autowired
    private SessionFactory session;

    @Override
    public void add(CategoriesModel categories) {
        session.getCurrentSession().save(categories);
        //this "categories" is a table name
    }

    @Override
    public void edit(CategoriesModel categories) {
        session.getCurrentSession().update(categories);
        //this "categories" is a table name
    }

    @Override
    public void delete(int id) {
        session.getCurrentSession().delete(getCategoriesModel(id));
        //this "id" is a feild in Model
    }

    @Override
    public CategoriesModel getCategoriesModel(int id) {
        return (CategoriesModel) session.getCurrentSession().get(CategoriesModel.class, id);
    }

    @Override
    public List getAllCategoriesModel() {
        return session.getCurrentSession().createQuery("from CategoriesModel").list();
         //this "CategoriesModel" is a its model name
    }

这是我的服务类

public void add(CategoriesModel categories);

    public void edit(CategoriesModel categories);

    public void delete(int id);

    public CategoriesModel getCategoriesModel(int id);

    public List getAllCategoriesModel();

这是我的服务impl类

@Service
public class CategoriesServiceImpl implements CategoriesService {

    @Autowired
    private CategoriesDao CategoriesDao;

    @Transactional
    public void add(CategoriesModel categories) {
        CategoriesDao.add(categories);
    }

    @Transactional
    public void edit(CategoriesModel categories) {
        CategoriesDao.edit(categories);
    }

    @Transactional
    public void delete(int id) {
        CategoriesDao.delete(id);
    }

    @Transactional
    public CategoriesModel getCategoriesModel(int id) {
        return CategoriesDao.getCategoriesModel(id);
    }

    @Transactional
    public List getAllCategoriesModel() {
        return CategoriesDao.getAllCategoriesModel();
    }

这是我的控制器类

@Autowired
    private CategoriesService CategoriesService;

    @RequestMapping("/")
    public String setupForm(Map<String, Object> map) {
        CategoriesModel categories = new CategoriesModel();
        //Create a new object from details Model
        map.put("category", categories);
        //new created object is assign and view name

        map.put("categoriesList", CategoriesService.getAllCategoriesModel());
        //view feild assign list in view page
        System.out.println(categories);
        return "allcategories";

        //return page(view name)
    }

这是我的观点

 <c:forEach items="${categoriesList}" var="category">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div id="category">
                <div class="col-lg-2 col-md-2 col-sm-4 col-xs-6 ">
                    <a href="" target="_self"><img src="images/properties/cars.png" class="img-responsive">
                        <div class="link">
                            <p>${category.category1}</p>
                        </div>
                </div>

1 个答案:

答案 0 :(得分:0)

@Autowired
    private CategoriesService CategoriesService;

    @RequestMapping("/")
    public String setupForm(Model model) {
        CategoriesModel categories = new CategoriesModel();
        //Create a new object from details Model
        model.addAttribute("category", categories);
        //new created object is assign and view name

        model.addAttribute("categoriesList", CategoriesService.getAllCategoriesModel());
        //view feild assign list in view page
        System.out.println(categories);
        return "allcategories";

        //return page(view name)
    }