J2ee - ch5 - Struts - Last 4 Topics
J2ee - ch5 - Struts - Last 4 Topics
In Apache Struts 2, actions are essentially Java objects responsible for processing user requests.
These actions, often referred to as ActionObjects, handle incoming requests, perform required
business logic, and return a result back to the user. Here's how you can create and use ActionObjects
in Struts 2:
For example:
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="myAction" class="com.example.MyAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
In this example, the myAction URL is mapped to the MyAction class. The SUCCESS result is mapped
to success.jsp, which will be displayed if the action's execute() method returns SUCCESS.
When the user accesses the URL, Struts 2 will invoke the execute() method of the corresponding
action class (MyAction in this case). The action processes the request, and the SUCCESS result
renders the success.jsp page.
<h2>Form Data</h2>
Username: <c:out value="${userForm.username}" /><br />
Password: <c:out value="${userForm.password}" /><br />
In this example, ${userForm.username} and ${userForm.password} access the properties of the form
bean, displaying the submitted data.
By using form beans, you can easily manage form data, perform validation, and keep your code
modular and maintainable. Remember that this example demonstrates a servlet-based approach,
but the concept can be applied in various Java web frameworks.
<h2>Form Data</h2>
Username: <c:out value="${userForm.username}" /><br />
Password: <c:out value="${userForm.password}" /><br />
In this example, ${userForm.username} and ${userForm.password} access the properties of the form
bean, displaying the submitted data.
By using form beans, you can easily manage form data, perform validation, and keep your code
modular and maintainable. Remember that this example demonstrates a servlet-based approach,
but the concept can be applied in various Java web frameworks.
(4) Using Properties Files
Using properties files in Java applications is a common practice for managing configuration settings,
internationalization (i18n), and other constant values. Properties files store key-value pairs, making
it easy to read and modify configurations without altering the source code. Here's how you can use
properties files in a Java application:
# Database Configuration
db.url=jdbc:mysql://localhost:3306/mydatabase
db.username=myuser
db.password=mypassword
# Application Settings
app.version=1.0
app.debugMode=false
import java.io.InputStream;
import java.util.Properties;
public ConfigLoader() {
try (InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties"))
{ properties.load(input); }
catch (Exception e)
{
e.printStackTrace(); // Handle the exception according to your application's needs
}
}
Using properties files in this way allows you to centralize your configuration settings, making it easier
to modify application behavior without modifying the source code.