【问题标题】:Update functionality not working as expected更新功能未按预期工作
【发布时间】:2018-06-27 09:56:52
【问题描述】:

我是 Spring MVC 和 Hibernate 的新手。我写了一个简单的应用程序,但我没有发现这段代码有什么问题。

更新功能无法正常工作。 Upto GET 方法工作正常,下面是相同的 URL:

http://localhost:8080/TestSample/forms/updateReq/1

单击提交按钮后,POST 方法不起作用。这是链接:

http://localhost:8080/TestSample/forms/updateReq/addReqNo

这是我的控制器:

package com.sample.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.objenesis.instantiator.perc.PercSerializationInstantiator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sample.Service.AddMoreService;
import com.sample.model.AddMoreList;
import com.sample.model.AddMoreModel;
import com.sample.model.AddNoOfReq;

@Controller
public class AddMoreFieldController {

    @Autowired
    private AddMoreService addMoreService;

    @RequestMapping(value = "/addReqNo", method = RequestMethod.GET)
    public String testPage(AddNoOfReq addNoOfReq, Model model) {
        model.addAttribute("addNoOfReq", addNoOfReq);
        model.addAttribute("reqList", addMoreService.getReq(addNoOfReq));
        return "testPage";
    }

    @RequestMapping(value = "/addReqNo", method = RequestMethod.POST)
    public String testPageProcess(@ModelAttribute AddNoOfReq addNoOfReq, Model model) {
        for (int i = 0; i <= addNoOfReq.getReqName(); i++) {
            if (addNoOfReq.getReqNo() == 0) {
                addMoreService.addReq(addNoOfReq);
            } else {
                addMoreService.updateReq(addNoOfReq);
            }
        }
        model.addAttribute("addNoOfReq", addNoOfReq);

        model.addAttribute("reqList", addMoreService.getReq(addNoOfReq));
        return "redirect:/forms/testPage";
    }

    @RequestMapping(value = "/updateReq/{reqNo}", method = RequestMethod.GET)
    public String editReq(@PathVariable("reqNo") int reqNo, Model model) {

        AddNoOfReq addNoOfReq = new AddNoOfReq();
        model.addAttribute("addNoOfReq", addMoreService.findElementById(reqNo));
        model.addAttribute("reqList", addMoreService.getReq(addNoOfReq));

        return "testPage";
    }
}

这是我的 DAO:

package com.sample.Dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sample.model.AddMoreModel;
import com.sample.model.AddNoOfReq;

@Repository
public class AddMoreDaoImpl implements AddMoreDao {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    protected Session getSession() {
        return sessionFactory.openSession();
    }

    @Override
    public void add(AddMoreModel addMoreModel) {

    }

    @Override
    public void addReq(AddNoOfReq addNoOfReq) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        if (addNoOfReq != null) {
            session.saveOrUpdate(addNoOfReq);

            tx.commit();

            session.close();
        }
    }

    @Override
    public AddNoOfReq findElementById(int rId) {
        AddNoOfReq addNoOfReq = (AddNoOfReq) getSession().get(AddNoOfReq.class, rId);
        return addNoOfReq;
    }

    @Override
    public void updateReq(AddNoOfReq addNoOfReq) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        if (addNoOfReq != null) {
            session.update(addNoOfReq);

            tx.commit();

            session.close();
        }

    }

    @Override
    public List getReq(AddNoOfReq addNoOfReq) {
        Session session = sessionFactory.openSession();
        List<AddNoOfReq> list = new ArrayList<>();
        Query query = session.createQuery("from AddNoOfReq");

        list = query.list();
        session.close();
        return list;
    }
}

【问题讨论】:

  • 您是否遇到任何异常或错误?
  • 不,我没有收到任何异常,但我收到服务器 405 状态,因为不支持请求方法“POST”
  • 代替 @ModelAttribute ,使用 @Requestbody
  • 感谢您的建议,但仍然无效。

标签: spring rest spring-data


【解决方案1】:

它不起作用,因为您没有此路径的映射:

http://localhost:8080/TestSample/forms/updateReq/addReqNo

要在这个 URL 上执行 POST 方法,你需要如下函数:

@RequestMapping(value = "/updateReq/{reqNo}", method = RequestMethod.POST)
public String editReq(@PathVariable("reqNo") int reqNo, Model model) {

    AddNoOfReq addNoOfReq = new AddNoOfReq();
    model.addAttribute("addNoOfReq", addMoreService.findElementById(reqNo));
    model.addAttribute("reqList", addMoreService.getReq(addNoOfReq));

    return "testPage";
}

但只有 method = RequestMethod.GET 有相同的功能。

【讨论】:

  • 我在同一个表单上重定向,所以我调用同一个 POST 方法来更新值。
  • 一般来说,如果你想更新一些东西(函数名 editReq 建议)你应该只使用 POST/PUT 方法。即使您使用相同的 URL,在您的 REST API 中,您也应该使用 GET 方法,仅接收一些不更新的信息,并根据其规范使用另一种方法。在这种情况下:我看到您的 editReq 方法没有编辑任何内容,因此它是 GET 请求。名称“editReq”和 url 路径“/updateReq/​​{reqNo}”具有误导性。 (正如我所说,如果您确实需要更新某些内容,请更改“方法”属性)。
  • 我明白了。谢谢
猜你喜欢
  • 2013-08-29
  • 2020-04-28
  • 2021-08-27
  • 1970-01-01
  • 2012-10-07
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多