当前位置: 首页 > 知识库问答 >
问题:

Struts2、Spring和hibernate集成:Struts2的模型类变量始终为空

韦鸣
2023-03-14

当我运行应用程序时,它总是在action类中的model类变量中返回null。

以下是片段

类:StudentPersonalDetails.java

@Entity
@Table(name="STUDENT_PERSONAL_DETAILS")
public class StudentPersonalDetails {

@Id
@Column(name="Student_ID", unique=true, nullable=false, length=20)
private String StudentId;

@Column(name="FIRST_NAME", nullable=false, length=255)
private String FirstName;

@Column(name="MIDDLE_NAME", nullable=false, length=255)
private String MiddleName;

@Column(name="LAST_NAME", nullable=false, length=255)
private String LastName;

@Column(name="GENDER", nullable=false, length=255)
private String Gender;

@Temporal(TemporalType.DATE)
@Column(name="DATE_OF_BIRTH", nullable=false, length=10)
private Date DateOfBirth;

@Column(name="GUARDIAN", nullable=false, length=255)
private String Guardian;

@Column(name="GUARDIAN_FIRST_NAME", nullable=false, length=255)
private String GuardianFirstName;

@Column(name="GUARDIAN_MIDDLE_NAME", nullable=false, length=255)
private String GuardianMiddleName;

@Column(name="GUARDIAN_LAST_NAME", nullable=false, length=255)
private String GuardianLastName;

@Column(name="STATE_OF_DOMICILE", nullable=false, length=255)
private String StateOfDomicile;

@Column(name="NATIONALITY", nullable=false, length=255)
private String Nationality;

@Column(name="RELIGION", nullable=false, length=255)
private String Religion;

@Column(name="STUDENT_CATEGORY", nullable=false, length=255)
private String Category;

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="highestQualification",column=@Column(name="STUDENT_HIGHEST_QUALIFICATION")),
    @AttributeOverride(name="yearOfAppearence",column=@Column(name="YEAR_OF_APPEARENCE")),
    @AttributeOverride(name="monthOfAppearence",column=@Column(name="MONTH_OF_APPEARENCE"))

})
private QualificationDetails qualificationDetails = new QualificationDetails();

//  @ElementCollection
//  @JoinTable(name="STUDENT_CONTACT_DETAILS",joinColumns=@JoinColumn(name="STUDENT_ID"))
//  @GenericGenerator(name = "hilo-gen", strategy = "hilo")
//  @CollectionId(columns = { @Column(name="CONTACT_DETAILS_ID") }, generator = "hilo-gen", type     = @Type(type="long"))
//  private Collection<ContactDetails> contactDetails = new ArrayList<ContactDetails>();


@Embedded
private ContactDetails contactDetails = new ContactDetails();


public ContactDetails getContactDetails() {
    return contactDetails;
}
public void setContactDetails(ContactDetails contactDetails) {
    this.contactDetails = contactDetails;
}
public String getStudentId() {
    return StudentId;
}
public String getFirstName() {
    return FirstName;
}
public String getMiddleName() {
    return MiddleName;
}
public String getLastName() {
    return LastName;
}
public String getGender() {
    return Gender;
}
public Date getDateOfBirth() {
    return DateOfBirth;
}
public String getGuardian() {
    return Guardian;
}
public String getGuardianFirstName() {
    return GuardianFirstName;
}
public String getGuardianMiddleName() {
    return GuardianMiddleName;
}
public String getGuardianLastName() {
    return GuardianLastName;
}
public String getStateOfDomicile() {
    return StateOfDomicile;
}
public String getNationality() {
    return Nationality;
}
public String getReligion() {
    return Religion;
}
public String getCategory() {
    return Category;
}
public void setStudentId(String studentId) {
    StudentId = studentId;
}
public void setFirstName(String firstName) {
    FirstName = firstName;
}
public void setMiddleName(String middleName) {
    MiddleName = middleName;
}
public void setLastName(String lastName) {
    LastName = lastName;
}
public void setGender(String gender) {
    Gender = gender;
}
public void setDateOfBirth(Date dateOfBirth) {
    DateOfBirth = dateOfBirth;
}
public void setGuardian(String guardian) {
    Guardian = guardian;
}
public void setGuardianFirstName(String guardianFirstName) {
    GuardianFirstName = guardianFirstName;
}
public void setGuardianMiddleName(String guardianMiddleName) {
    GuardianMiddleName = guardianMiddleName;
}
public void setGuardianLastName(String guardianLastName) {
    GuardianLastName = guardianLastName;
}
public void setStateOfDomicile(String stateOfDomicile) {
    StateOfDomicile = stateOfDomicile;
}
public void setNationality(String nationality) {
    Nationality = nationality;
}
public void setReligion(String religion) {
    Religion = religion;
}
public void setCategory(String category) {
    Category = category;
}

public QualificationDetails getQualificationDetails() {
    return qualificationDetails;
}
public void setQualificationDetails(QualificationDetails qualificationDetails) {
    this.qualificationDetails = qualificationDetails;
}

}

类:ContactDetails.java

@Embeddable
public class ContactDetails {

@Column(name="MOBILE_NUMBER", nullable=false, length=255)
private String MobileNumber;

@Column(name="ALTERNATE_NUMBER", nullable=false, length=255)
private String AlternateNumber;

@Column(name="EMAIL_ADDRESS", nullable=false, length=255)
private String EmailAddress;

@Column(name="STREET", nullable=false, length=255)
private String Street;  

@Column(name="POST_OFFICE", nullable=false, length=255)
private String PostOffice;

@Column(name="CITY", nullable=false, length=255)
private String City;

@Column(name="PIN_CODE", nullable=false, length=255)
private String PinCode;

@Column(name="STATE", nullable=false, length=255)
private String State;

public String getMobileNumber() {
    return MobileNumber;
}

public String getAlternateNumber() {
    return AlternateNumber;
}

public String getEmailAddress() {
    return EmailAddress;
}

public String getStreet() {
    return Street;
}

public String getPostOffice() {
    return PostOffice;
}

public String getCity() {
    return City;
}

public String getPinCode() {
    return PinCode;
}

public String getState() {
    return State;
}

public void setMobileNumber(String mobileNumber) {
    MobileNumber = mobileNumber;
}

public void setAlternateNumber(String alternateNumber) {
    AlternateNumber = alternateNumber;
}

public void setEmailAddress(String emailAddress) {
    EmailAddress = emailAddress;
}

public void setStreet(String street) {
    Street = street;
}

public void setPostOffice(String postOffice) {
    PostOffice = postOffice;
}

public void setCity(String city) {
    City = city;
}

public void setPinCode(String pinCode) {
    PinCode = pinCode;
}

public void setState(String state) {
    State = state;
}

}

类:QualificationDetails.java

@Embeddable
public class QualificationDetails {

@Column(name="HIGHEST_QUALIFICATION", nullable=false, length=255)
private String highestQualification;

@Column(name="MONTH_OF_APPEARENCE", nullable=false, length=255)
private String monthOfAppearence;

@Column(name="YEAR_OF_APPEARENCE", nullable=false, length=255)
private String yearOfAppearence;



public String getHighestQualification() {
    return highestQualification;
}

public String getYearOfAppearence() {
    return yearOfAppearence;
}

public void setHighestQualification(String highestQualification) {
    this.highestQualification = highestQualification;
}

public void setYearOfAppearence(String yearOfAppearence) {
    this.yearOfAppearence = yearOfAppearence;
}

public String getMonthOfAppearence() {
    return monthOfAppearence;
}

public void setMonthOfAppearence(String monthOfAppearence) {
    this.monthOfAppearence = monthOfAppearence;
}

}

类:IMPLStudentsDAO.java

public class ImplStudentsDao implements StudentsDAO{

@Autowired
private SessionFactory sf;

@Override
public List<StudentPersonalDetails> allStudentsList() throws Exception {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void Delete(int StudentId) throws Exception {
    try
    {

    }
    catch(Exception ex)
    {
        throw new Exception("CLASS - ImplStudentPersonalDetailsDao : Method - SAVE : MESSEGE - " +ex);
    }

}

@Override
public StudentPersonalDetails FindById(int StudentId) throws Exception {
    try
    {

    }
    catch(Exception ex)
    {
        throw new Exception("CLASS - ImplStudentPersonalDetailsDao : Method - SAVE : MESSEGE - " +ex);
    }
    return null;

}

@Override
public void Save(StudentPersonalDetails spi) throws Exception {

    try
    {
        this.sf.getCurrentSession().saveOrUpdate(spi);
    }
    catch(Exception ex)
    {
        throw new Exception("CLASS - ImplStudentPersonalDetailsDao : Method - SAVE : MESSEGE - " +ex);
    }

}

@Override
public void Update(StudentPersonalDetails spi) throws Exception {
    try
    {

    }
    catch(Exception ex)
    {
        throw new Exception("CLASS - ImplStudentPersonalDetailsDao : Method - UPDATE : MESSEGE - " +ex);
    }

}

public void setSf(SessionFactory sf) {
    this.sf = sf;
}


}
<hibernate-configuration>

<session-factory>

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/studentTest</property>
    <property name="connection.username">root</property>
    <property name="connection.password">pass</property> 
    <property name="connection.pool_size">1</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="org.Students.Model.StudentPersonalDetails"/>

</session-factory>

</hibernate-configuration>

bean配置:spring-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

      <bean name="StudentPersonalDetailsBean" class="org.Students.Model.StudentPersonalDetails">
        <property name="contactDetails" ref="contactDetailsBean"/>
        <property name="qualificationDetails" ref="qualificationDetailsBean"/>
    </bean>

     <bean name="contactDetailsBean" class="org.Students.Model.ContactDetails"/>

    <bean name="qualificationDetailsBean" class="org.Students.Model.QualificationDetails"/>

    <bean name="ImplStudentDaoBean" class="org.Students.Dao.Impl.ImplStudentsDao">
        <property name="sf" >
            <ref bean="sessionFactory"/>
        </property>

    </bean>

    <bean name="ImplStudentsBoBean" class="org.Students.BO.Impl.ImplStudentsBO">
        <property name="stdDao" >
            <ref bean="ImplStudentDaoBean"/>

        </property>

    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/studentTest" />
      <property name="username" value="root" />
      <property name="password" value="pass" />
   </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="packagesToScan" value="org.Students.Model"></property>
    <property name="hibernateProperties">
        <props>
        <prop key="connection.pool_size">1</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

</bean>

<tx:annotation-driven />
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />

</bean>
</beans>

JSP:ApplicationForm.JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration Form</title>
</head>
<body>

<s:form method="post" action="addStudentAction">

<s:textfield name="spd.StudentId" label="ID" />
<s:textfield name="spd.FirstName" label="First Name" />
<s:textfield name="spd.MiddleName" label="Middle Name" />
<s:textfield name="spd.LastName" label="Last Name" />
<s:textfield name="spd.Gender" label="Gender" />
<s:textfield name="spd.DateOfBirth" label="Date Of Birth" />
<s:textfield name="spd.Guardian" label="Guardian" />
<s:textfield name="spd.GuardianFirstName" label="Guardian First Name" />
<s:textfield name="spd.GuardianMiddleName" label="Guardian Middle Name"/>
<s:textfield name="spd.GuardianLastName" label="Guardian Last Name" />
<s:textfield name="spd.StateOfDomicile" label="State Of Domicile"/>
<s:textfield name="spd.Nationality" label="Nationality" />
<s:textfield name="spd.Religion" label="Religion" />
<s:textfield name="spd.Category" label="Category" />

<s:textfield name="qd.highestQualification" label="Highest Qualification" />
<s:textfield name="qd.monthOfAppearence" label="Month Of Appearence" />
<s:textfield name="qd.yearOfAppearence" label="Year Of Appearence"/>

<s:textfield name="cd.MobileNumber" label="Mobile Number" />
<s:textfield name="cd.AlternateNumber" label="Alternate Number"/>
<s:textfield name="cd.EmailAddress" label="Email Address" />
<s:textfield name="cd.Street" label="Street" />
<s:textfield name="cd.PostOffice" label="Post Office"/>
<s:textfield name="cd.City" label="City"/>
<s:textfield name="cd.PinCode" label="Pin Code"/>
<s:textfield name="cd.State" label="State"/>

<s:submit />

</s:form>

</body>

</html>

类:ApplicationAction.java

public class ApplicationAction extends ActionSupport implements Preparable {


    private static final long serialVersionUID = 1L;

    private static final Logger logger = Logger.getLogger(ApplicationAction.class);
    private StudentPersonalDetails spd ;
    private ContactDetails cd ;
    private QualificationDetails qd ;

    private StudentsBO Sbo ;

    public String execute()
    {
        System.out.println("Registration Successful!");
        return "success";
    }

    public void validate()
       {

        System.out.println("validate Method Called");
        spd = new StudentPersonalDetails();

            System.out.println(spd.getStudentId());
            System.out.println(spd.getFirstName());
            System.out.println(spd.getMiddleName());
            System.out.println(spd.getLastName());

            System.out.println(spd.getGender());
            System.out.println(spd.getDateOfBirth());
            System.out.println(spd.getGuardian());

            System.out.println(spd.getGuardianFirstName());
            System.out.println(spd.getGuardianMiddleName());
            System.out.println(spd.getGuardianLastName());

            System.out.println(spd.getStateOfDomicile());
            System.out.println(spd.getNationality());

            System.out.println(spd.getReligion());
            System.out.println(spd.getCategory());

            System.out.println(qd.getHighestQualification());
            System.out.println(qd.getMonthOfAppearence());
            System.out.println(qd.getYearOfAppearence());

            System.out.println(cd.getAlternateNumber());
            System.out.println(cd.getCity());
            System.out.println(cd.getEmailAddress());
            System.out.println(cd.getMobileNumber());
            System.out.println(cd.getPinCode());
            System.out.println(cd.getPostOffice());
            System.out.println(cd.getState());
            System.out.println(cd.getStreet());

         if (spd.getStudentId() == null || spd.getStudentId().trim().equals(""))
          {
             addFieldError("spd.StudentId","The Student Id is required");
          }

          if (spd.getFirstName() == null || spd.getFirstName().trim().equals(""))
          {
             addFieldError("spd.FirstName","The First Name is required");
          }

          if (spd.getMiddleName() == null || spd.getMiddleName().trim().equals(""))
          {
             addFieldError("spd.MiddleName","The Middle Name is required");
          }

          if (spd.getLastName() == null || spd.getLastName().trim().equals(""))
          {
             addFieldError("spd.LastName","The Last Name is required");
          }

          if (spd.getGender() == null || spd.getGender().trim().equals(""))
          {
             addFieldError("spd.Gender","The Gender is required");
          }

          if (spd.getDateOfBirth() == null || spd.getDateOfBirth().toString().trim().equals(""))
          {
             addFieldError("spd.DateOfBirth","The Date Of Birth is required");
          }

          if (spd.getGuardian() == null || spd.getGuardian().trim().equals(""))
          {
             addFieldError("spd.Guardian","The Guardian is required");
          }

          if (spd.getGuardianFirstName() == null || spd.getGuardianFirstName().trim().equals(""))
          {
             addFieldError("spd.GuardianFirstName","The Guardian FirstName is required");
          }

          if (spd.getGuardianMiddleName() == null || spd.getGuardianMiddleName().trim().equals(""))
          {
             addFieldError("spd.GuardianMiddleName","The Guardian MiddleName is required");
          }

          if (spd.getGuardianLastName() == null || spd.getGuardianLastName().trim().equals(""))
          {
             addFieldError("spd.GuardianLastName","The Guardian LastName is required");
          }

          if (spd.getStateOfDomicile() == null || spd.getStateOfDomicile().trim().equals(""))
          {
             addFieldError("spd.StateOfDomicile","The State Of Domicile is required");
          }

          if (spd.getNationality() == null || spd.getNationality().trim().equals(""))
          {
             addFieldError("spd.Nationality","The Nationality is required");
          }

          if (spd.getReligion() == null || spd.getReligion().trim().equals(""))
          {
             addFieldError("spd.Religion","The Religion is required");
          }

          if (spd.getCategory() == null || spd.getCategory().trim().equals(""))
          {
             addFieldError("spd.Category","The Category is required");
          }

          if (qd.getHighestQualification() == null || qd.getHighestQualification().trim().equals(""))
          {
             addFieldError("qd.highestQualification","Highest Qualification is required");
          }

          if (qd.getMonthOfAppearence() == null || qd.getMonthOfAppearence().trim().equals(""))
          {
             addFieldError("qd.monthOfAppearence","Month Of Appearence is required");
          }

          if (qd.getYearOfAppearence() == null || qd.getYearOfAppearence().trim().equals(""))
          {
             addFieldError("qd.yearOfAppearence","The Year Of Appearence is required");
          }

          if (cd.getAlternateNumber() == null || cd.getAlternateNumber().trim().equals(""))
          {
             addFieldError("cd.AlternateNumber","The Alternate Number is required");
          }

          if (cd.getStreet() == null || cd.getStreet().trim().equals(""))
          {
             addFieldError("cd.Street","Street is required");
          }

          if (cd.getCity() == null || cd.getCity().trim().equals(""))
          {
             addFieldError("cd.City","The City is required");
          }

          if (cd.getEmailAddress() == null || cd.getEmailAddress().trim().equals(""))
          {
             addFieldError("cd.EmailAddress","The Email Address is required");
          }

          if (cd.getMobileNumber() == null || cd.getMobileNumber().trim().equals(""))
          {
             addFieldError("cd.MobileNumber","The Mobile Number is required");
          }

          if (cd.getPinCode() == null || cd.getPinCode().trim().equals(""))
          {
             addFieldError("cd.PinCode","The Pin Code is required");
          }

          if (cd.getPostOffice() == null || cd.getPostOffice().trim().equals(""))
          {
             addFieldError("cd.PostOffice","The Post Office is required");
          }

          if (cd.getState() == null || cd.getState().trim().equals(""))
          {
             addFieldError("cd.State","The State is required");
          }

       }

    public String addStudent()
    { 
        System.out.println("addStudent Method Called");

        System.out.println(spd.getStudentId());
        System.out.println(spd.getFirstName());
        System.out.println(spd.getMiddleName());
        System.out.println(spd.getLastName());

        System.out.println(spd.getGender());
        System.out.println(spd.getDateOfBirth());
        System.out.println(spd.getGuardian());

        System.out.println(spd.getGuardianFirstName());
        System.out.println(spd.getGuardianMiddleName());
        System.out.println(spd.getGuardianLastName());

        System.out.println(spd.getStateOfDomicile());
        System.out.println(spd.getNationality());

        System.out.println(spd.getReligion());
        System.out.println(spd.getCategory());

        System.out.println(qd.getHighestQualification());
        System.out.println(qd.getMonthOfAppearence());
        System.out.println(qd.getYearOfAppearence());

        System.out.println(cd.getAlternateNumber());
        System.out.println(cd.getCity());
        System.out.println(cd.getEmailAddress());
        System.out.println(cd.getMobileNumber());
        System.out.println(cd.getPinCode());
        System.out.println(cd.getPostOffice());
        System.out.println(cd.getState());
        System.out.println(cd.getStreet());

        spd.setQualificationDetails(qd);
        spd.setContactDetails(cd);


        try {
            Sbo.saveOrUpdate(spd);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "success";
    }

    @Override
    public void prepare() throws Exception {

    }

    public void setSpd(StudentPersonalDetails spd) {
        this.spd = spd;
    }

    public void setCd(ContactDetails cd) {
        this.cd = cd;
    }

    public void setQd(QualificationDetails qd) {
        this.qd = qd;
    }

    public void setSbo(StudentsBO sbo) {
        Sbo = sbo;
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<import resource="classpath*:SusBusinessServiceLayerSpringConfig*.xml"/>

<bean name="ApplicationAction" class="org.Student.ActionClasses.ApplicationAction" scope="prototype">
     <property name="spd">
        <ref bean="StudentPersonalDetailsBean"/>
     </property>

</bean>
</beans>
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
   <display-name>Embedded Student Test</display-name>
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

    </web-app>

现在的问题是,当我在apache tomcat中部署war文件并填充jsp表单时,actionclass模型变量打印空值。我不明白为什么。

我忘记提了,这里是struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache software foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="messages" />

    <package name="default" extends="struts-default">

    <action name="addStudentAction" class="org.Student.ActionClasses.ApplicationAction" method="addStudent" >
            <result name="success">/ApplyOnline/RegistrationSucessful.jsp</result>
            <result name="input">/ApplyOnline/ApplicationForm.jsp</result>
            <result name="failure">/ApplyOnline/ApplicationForm.jsp</result>

        </action>

    </package>

</struts>

共有1个答案

段干河
2023-03-14

因为实例化模型类太晚了。您应该在params拦截器之前执行此操作。可以内联或在构造函数中执行,但不能在validate方法中执行。

private StudentPersonalDetails spd = new StudentPersonalDetails();
private ContactDetails cd = new ContactDetails() ;
private QualificationDetails qd = new QualificationDetails() ;

public void validate() {    
        System.out.println("validate Method Called");

spd=new StudentPersonalDetails();

编辑:

<bean id="ApplicationAction" class="org.Student.ActionClasses.ApplicationAction" scope="prototype"/>
<action name="addStudentAction" class="ApplicationAction">
 类似资料:
  • 主要内容:数据库设置:,Hibernate的配置:,环境设置:,Hibernate 类:,动作类:,创建视图文件:,Struts 配置:Hibernate是一个高性能的对象/关系持久性和查询服务许可下的开源GNU通用公共许可证(LGPL),并免费下载。在这一章中,我们要学习如何实现Struts2与Hibernate集成。如果你不熟悉与Hibernate,那么可以查看我们的Hibernate教程。 数据库设置: 在本教程中,我会使用“struts2_tutorial”MySQL数据库。我连接到我的

  • Spring是一个流行的Web框架,它提供易于集成与很多常见的网络任务。所以,问题是,为什么我们需要Spring,当我们有Struts2?Spring是超过一个MVC框架 - 它提供了许多其它好用的东西,这是不是在Struts。例如:依赖注入可以是有用的任何框架。在本章中,我们将通过一个简单的例子来看看如何集成Spring和Struts2一起。 首先,需要添加下列文件到项目的构建路径从Spring

  • 主要内容:1. 工程文件夹结构,2. MySQL表结构结构,3. Hibernate相关配置,5. Struts2相关,6. Spring相关配置,7. JSP 页面,8. struts.xml,9. Struts 2 + Spring,10. 运行实例,参考在本教程中,它显示的集成 “Struts2 + Spring + Hibernate“,请务必检查以下之前继续学习教程。 Struts2 + Hibernate集成实例 Struts2 + Spring 集成实例 参见集成步骤总结: 获取所

  • 主要内容:1. 工程目录结构,2. MySQL表结构脚本,4. Hibernate 相关配置,5. Hibernate ServletContextListener,6. Action,7. JSP 页面,8. struts.xml,9. 实例测试执行,参考在 Struts2 中,没有官方的插件集成Hibernate框架。但是,可以通过以下步骤解决方法: 注册一个自定义的 ServletContextListener 在 ServletContextListener 类, 初始化Hibernat

  • 在本章中,让我们通过Struts2的集成Tiles框架所涉及的步骤。 Apache的Tiles是一个内置的模板框架来简化Web应用程序用户界面的开发。 首先,我们需要从Apache Tiles 网站下载的files jar文件。需要添加下面的jar文件添加到项目的类路径。 tiles-api-x.y.z.jar tiles-compat-x.y.z.jar tiles-core-x.y.z.jar

  • 问题内容: 我正在开始一个项目,愿意使用Struts2和Hibernate。 我应该使用 struts2-full-hibernate 插件还是以其他方式集成它们? 在Internet上搜索使我感到困惑:这是整合它们的标准方法吗?如果没有,这是标准方法? 问题答案: 简而言之: 选择一个前端框架(通常是MVC,然后是 Struts2 , JSF2 , Spring MVC 等等),您已经选择了 S