How can we access Java Beans and their properties in Struts?
In Struts, JavaBeans are used to encapsulate data and represent the model in the MVC (Model-View-Controller) architecture. Accessing JavaBeans and their properties in Struts involves several steps, including defining the JavaBean, configuring it in the Struts configuration file, and using it in JSP pages. Here’s a step-by-step guide on how to do this:
First, you need to create a JavaBean class. A JavaBean is a simple Java class that follows certain conventions, such as having a public no-argument constructor, providing getter and setter methods for its properties, and being serializable.
public class User {
private String name;
private String email;
// No-argument constructor
public User() {}
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
In Struts, you typically use an ActionForm
to handle form data. You can create an ActionForm
that corresponds to your JavaBean.
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm {
private String name;
private String email;
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
You need to define the ActionForm
in the struts-config.xml
file and map it to an action.
<action-mappings>
<action path="/userForm"
type="com.example.UserAction"
name="userForm"
scope="request"
input="/userForm.jsp">
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
</action-mappings>
<form-beans>
<form-bean name="userForm" type="com.example.UserForm"/>
</form-beans>
Create an action class that processes the form submission.
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class UserAction extends org.apache.struts.action.Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
// Access properties from the form
String name = userForm.getName();
String email = userForm.getEmail();
// Business logic here
return mapping.findForward("success");
}
}
In your JSP page, you can access the properties of the JavaBean using the Struts tag library.
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>User Form</title>
</head>
<body>
<html:form action="/userForm">
<html:text property="name" />
<html:text property="email" />
<html:submit value="Submit" />
</html:form>
</body>
</html>
ActionForm
and includes properties corresponding to the JavaBean.struts-config.xml
.By following these steps, you can effectively access JavaBeans and their properties in a Struts application.