如何从spring控制器jo jsp页面更新对象?

时间:2016-10-06 13:58:34

标签: java spring jsp

问题在于:我必须更新先前在其数据库表中输入的对象结果的答案字段,但在更新而不是升级时,您创建新结果anzichiè更新所选。也许两个控制器之间存在结果参数的传递问题?

我该如何解决?

ResultDaoImpl.java

@Autowired
    private SessionFactory sessionFactory;
    public void updateResult(Result result) {
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.merge(result);
            session.getTransaction().commit();

        }
public List<Result> findTaskResults(Task task) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List<Result> results =  (List<Result>) session.createQuery("select r from Result r where r.task.id="+task.getId()).list();
        session.getTransaction().commit();
        System.out.println(results);
        return results;
    }

TaskController.java

@RequestMapping(value="/newTask", method = RequestMethod.GET)
    public String task(@ModelAttribute Task task,@ModelAttribute Job job,@ModelAttribute Result result,
            @ModelAttribute Image image ,Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String s = auth.getName();
        Student student = studentFacade.retrieveUser(s);
        List<Task> tasks = taskFacade.retriveAllTask();
        task = getMathRandomList(tasks);

        if(task.getStudent() == null){
            task.setStudent(student);
            taskFacade.updateTask(task);
        }

        List<Result> results = resultFacade.findTaskResult(task);
        for(int i = 0; i<results.size();i++) {
            result = results.get(i);
        }

        model.addAttribute("result", result);


        model.addAttribute("task", task);

        return "users/newTask";
    }

    @RequestMapping(value="/taskRecap", method = RequestMethod.POST)
    public String taskRecap(@ModelAttribute Task task,@ModelAttribute Result result, Model model) {

            resultFacade.updateResult(result);
        return "users/taskRecap";
    }

newTask.jsp

<form:form method="post" action="taskRecap" modelAttribute="result" name="form">
    <c:forEach var="result" items="${task.results}"><br>
            <c:out value="${result.image.id}"/>
            <form:input type="text" path='answer' placeholder="Answer"/>

             <br>
    </c:forEach>                        
    <td><input type="submit" value="Send"></td>

    </form:form>

0 个答案:

没有答案