【问题标题】:how can I add several object in hibernate in one request using a clients如何使用客户端在一个请求中在休眠中添加多个对象
【发布时间】:2015-03-15 15:12:07
【问题描述】:

在我的 java web 应用程序中,我有 3 个实体想通过 jsp 客户端向这些实体插入数据,每个实体将有一个单独的 jsp 页面:我的目标是使用 hibernate 按需插入所有三个实体。 我该怎么做呢。目前在看spring MVC 任何帮助表示赞赏

这是我的控制器

    public class DriverControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        float gear = Float.parseFloat( request.getParameter("gear"));
        float increaeRPMX = Float.parseFloat(request.getParameter("RPM"));
        float decreaseRPMX = Float.parseFloat(request.getParameter("DRPM"));
        float prefMaxSpeedX = Float.parseFloat(request.getParameter("PMxSpeed"));
        float prefMinSpeedX = Float.parseFloat(request.getParameter("PMnSpeed"));
        float decisionTimeX = Float.parseFloat(request.getParameter("DTime"));
        float maxAccelrationX = Float.parseFloat(request.getParameter("MaxAccn"));
        float emaxBreakingmailX = Float.parseFloat(request.getParameter("MaxBreaking"));
        float maxStraighSpeedX = Float.parseFloat(request.getParameter("MSSpeed"));
        float maxCornerSpeedX = Float.parseFloat(request.getParameter("MCSpeed"));

        HttpSession session = request.getSession(true);
        try {
            DriverPropDAO driverDAO = new DriverPropDAO();
            driverDAO.addDriverPropDetails(gear,increaeRPMX, decreaseRPMX, prefMaxSpeedX, prefMinSpeedX, decisionTimeX, maxAccelrationX, emaxBreakingmailX, maxStraighSpeedX, maxCornerSpeedX);
                       response.sendRedirect("Success");
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}

这是我的 DAO

public class DriverPropDAO {

    public void addDriverPropDetails(float gear,float incRPM,float decRPM,float pMaxSpeed,float pMinSpeed,
            float dTime,float mAccelration,float mBreakingmail,float mStraighSpeed,float mCornerSpeed)  {
        try{

            // 1. configuring hibernate
            Configuration configuration = new Configuration().configure();

            // 2. create sessionfactory
            SessionFactory sessionFactory = configuration.buildSessionFactory();

            // 3. Get Session object
            Session session = sessionFactory.openSession();
         // 4. Starting Transaction
            Transaction transaction = session.beginTransaction();
            DriverProp driver = new DriverProp();
            driver.setGear(gear);
            driver.setRPM(incRPM);
            driver.setDecreaseRPM(decRPM);
            driver.setEmaxBreakingmail(mBreakingmail);
            driver.setMaxAccelration(mAccelration);
            driver.setMaxCornerSpeed(mCornerSpeed);
            driver.setMaxStraighSpeed(mStraighSpeed);
            driver.setPrefMaxSpeed(pMaxSpeed);
            driver.setPrefMinSpeed(pMinSpeed);
            driver.setDecisionTime(dTime); 

            session.save(driver);
            transaction.commit();
            System.out.println("\n\n Details Added \n");

        }
        catch(HibernateException e){
            System.out.println(e.getMessage());
            System.out.println("error");
        }
    }

}

【问题讨论】:

  • 你尝试了什么?
  • 目前我可以将对象单独保存在单独的页面中并将它们提交到页面
  • 我知道在 spring-mvc 中有更简洁的方法可以做到这一点,如果我能得到方向会很棒
  • 在下面查看我的答案

标签: java hibernate jsp spring-mvc


【解决方案1】:

我该怎么做。目前在看spring MVC

在 spring mvc 中,你有一个 @Controller 来处理请求和响应,以及 @ModelAttribute/command 对象来绑定来自 JSP 页面的数据。并且有一个@RequestMapping注解用于定义处理哪个url请求,

不要使用new 操作符创建新的服务实例和DAO 类,作为单例bean 添加到spring 容器中,并在需要时使用@Autowired@Inject@Resource 询问

喜欢:

@Controller
public class DriverController {

   @Autowired private DriverPropDAO driverDAO;

   @RequestMapping(value="/driver", method=RequestMethod.GET)
   public String getDriverForm(Model model){
       model.addAttribute("driver", new DriverProp());
   }

   @RequestMapping(value="/driver", method=RequestMethod.POST)
   public String processDriverForm(Model model, @ModelAttribute("driver")DriverProp driverProp, BindingResult result){

       //Here, validate driverProp intance, if fails return back

       //Here, save it.
       driverDAO.save(driverProp);

       return "redirect:/driver";
   }
}

并在 JSP 页面中使用 spring 表单标签,如:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form method="post" modelAttribute="driver">
    <form:input path="gear"/>
     .
     .
     .
</form:form>

另请参阅:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多