Chce stworzyć testowy program który będzie dodawał do bazy tresc zadan i będe mogł je pobierać z bazy. Chciałbym żeby było to zrobione z obsługą Rest bez .jsp. Mój kod nie chce działać i nie wiem jak go dokonczyc.
Controller
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yodo.controller;
import com.yodo.model.Task;
import com.yodo.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* @author piotr
*/
@Controller
public class TaskController {
@Autowired
TaskService service;
/**
*
* @param model
* @return
*/
@RequestMapping(value = "/tasks", method=RequestMethod.GET)
public String tasks( Model model){
model.addAttribute("task", service.findAllTasks());
model.addAttribute("newTask", new Task());
return "tasks";
}
@RequestMapping( value ="/add-task", method = RequestMethod.POST )
public String saveTask( Task task){
service.addTask(task);
return "redirect:/tasks";
}
}
Task
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yodo.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
/**
*
* @author Piotr
*/
@Entity
@Table(name = "tasks", catalog = "senfino", uniqueConstraints = {
@UniqueConstraint(columnNames = "id"),
@UniqueConstraint(columnNames = "subject")
})
public class Task implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO )
@Column(name = "id")
private int taskId;
@Column(name = "subject")
private String subject;
public Task(){}
public Task( int id, String subject){
this.taskId = id;
this.subject = subject;
}
public int getId() {
return taskId;
}
public void setId(int id) {
this.taskId = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
TaskService
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yodo.service;
import com.yodo.dao.TaskDAO;
import com.yodo.model.Task;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
/**
*
* @author Piotr
*/
public class TaskService implements TaskServ{
@Autowired
private TaskDAO taskDao;
@Override
public void addTask(Task task){
taskDao.saveTask(task);
}
@Override
public List<Task> findAllTasks(){
return taskDao.findAllTasks();
}
}
TaskDAO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yodo.dao;
import com.yodo.model.Task;
import java.util.List;
/**
*
* @author Piotr
*/
public interface TaskDAO {
public abstract void saveTask(Task task);
List<Task> findAllTasks() ;
}
TaskDAOImp
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yodo.dao;
import com.yodo.model.Task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
/**
*
* @author
*/
@Repository
public class TaskDaoImp extends HibernateDaoSupport implements TaskDAO{
public Map<Long,Task> sTask = new HashMap<Long, Task>();
@Override
public void saveTask(Task task) {
getHibernateTemplate().save(task);
}
@Override
public List<Task> findAllTasks() {
return new ArrayList<Task> (sTask.values());
}
}