gpt4 book ai didi

java - Struts 2 中的多表单执行

转载 作者:行者123 更新时间:2023-11-28 22:31:17 27 4
gpt4 key购买 nike

我正在开发 Struts 2 和 Hibernate 集成应用程序,我必须在其中制作员工注册系统

  1. 员工姓名详情JSP页面
  2. 员工教育详情页面。
  3. 员工资格详情

我将这些页面放在彼此的成功页面上,以便页面上的成功注册显示其他页面的形式但是在第一页中输入值后它完全运行成功并显示第二页是教育详细信息但是当我投入值(value)并提交。它不起作用,我收到错误。

java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity
org.hibernate.event.spi.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:62)
org.hibernate.event.spi.SaveOrUpdateEvent.<init>(SaveOrUpdateEvent.java:45)
org.hibernate.internal.SessionImpl.save(SessionImpl.java:748)
org.hibernate.internal.SessionImpl.save(SessionImpl.java:744)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
$Proxy13.save(Unknown Source)
dao.Edudao.add(Edudao.java:13)
action.Eduaction.execute(Eduaction.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devmode" value="true"/>

<package name="loginmodel" extends ="struts-default">
<action name ="employee" class ="action.Empaction">
<result name = "input">/EMP/employee.jsp</result>
<result name ="success">/EMP/empedu.jsp</result>
</action>
<action name ="education" class="action.Eduaction">
<result name= "siccess">/EMP/success.jsp</result>

</action>

</package>
</struts>

我在 DAO 层使用 Hibernate

Edudao.java:

package dao;

import org.hibernate.Session;

import model.Empedu;

public class Edudao {
public Empedu add(Empedu model){
Session session1 = ConnectionFactory.getSessionFactory().getCurrentSession();


session1.beginTransaction();
session1.save(model);

session1.getTransaction().commit();
return model;

}

}

Eduaction.class 是:

package action;

import model.Empedu;


import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.Edudao;

public class Eduaction extends ActionSupport implements ModelDriven<Empedu> {

private static final long serialVersionUID = 1L;
private Empedu model;

public Empedu getModelobj() {
return model;
}
public void setModelobj(Empedu model) {
this.model = model;
}
public String execute()throws Exception{
Edudao edudao = new Edudao();
Empedu queryresult = edudao.add(getModelobj());
if(queryresult!=null){
return SUCCESS;
}
else
return ERROR;
}
@Override
public Empedu getModel() {
// TODO Auto-generated method stub
return null;
}

}

最佳答案

Edudao.add() 中,您不应在实体的 null 或 transient 实例上使用 saveOrUpdate。使用 save 方法来保存 transient 实例。

将代码改为

public Empedu add(Empedu model) throws Exception {
if (model == null || model.getId() != null)
throw new IllegalArgumentException("null model, or has id not null");

Session session = ConnectionFactory.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();

try {
session.save(model);

tx.commit();
return model;
} finally{
if (session.isOpen()){
try {
ConnectionFactory.getSessionFactory().closeSession();
}
catch (Exception e2) {}
}
}
}

关于java - Struts 2 中的多表单执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com