在我的java web应用程序中我有3个实体想要通过jsp客户端向这些实体插入数据,每个实体将有一个单独的jsp页面:我的目标是使用hibernate在请求中插入所有三个实体。 我该怎么做呢目前正在关注春季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");
}
}
}
答案 0 :(得分:0)
我该怎么做呢。目前正在关注春季MVC
在spring mvc中,你有一个@Controller来处理请求和响应,而@ ModelAttribute / command对象来绑定来自JSP页面的数据。并且有一个@RequestMapping注释用于定义要处理的URL请求
不要使用new
运算符来创建新的服务实例和DAO类,将spring容器添加为单例bean,并在需要时使用@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 form标签,如:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form method="post" modelAttribute="driver">
<form:input path="gear"/>
.
.
.
</form:form>
另见: