Introduction💁
Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease. Java classes are compiled into byte code. The compiled byte codes can be used on any platform supporting Java without recompiling the class.
The modelDriven Interceptor is used to make other objects of the model as default objects for the value stack. The modelDriven Interceptor is present in the Apache Struts 2 framework.
There are no defined parameters for the modelDriven Interceptors.
Example 👀
Now that we have discussed the theory associated with the modelDriven Interceptor, let us look at a basic example to understand it better. The structure of the project would look something like this,
index.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit value="login"></s:submit>
</s:form>
The index.jsp file is used to create a basic form.
Login.java
package com.codingNinjas;
import com.opensymphony.xwork2.ModelDriven;
public class Login implements ModelDriven<User>{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public User getModel(){
user=new User();
return user;
}
public String execute(){
if(user.getPassword().equals("admin")){
return "success";
}
else{
return "error";
}
}
}
The Login.java page is used to fetch the name and the password from the index.jsp page. Then these details are verified against a given set of conditions. If the conditions are satisfied, the user is directed to the success page. Else the user is directed to the error page.
User.java
package com.codingNinjas;
public class User {
private String name,password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
This is the code that is used to create a model for the user.
login-error.jsp
<p>Incorrect Password!!</p>
<jsp:include page="index.jsp"></jsp:include>
The user is redirected to this page in case the correct credentials are not entered.
login-success.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
Welcome, <s:property value="name"/>
In case the user provides correct credentials, they are redirected to this page.
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="abc" extends="struts-default" >
<action name="login" class="com.codingNinjas.Login">
<result name="success" >/login-success.jsp</result>
<result name="error">/login-error.jsp</result>
</action>
</package>
</struts>
Output
Must Read Apache Server, Why is Java Platform Independent
Frequently Asked Questions
What is JavaFX, and how can we run a JavaFX code?
JavaFX is a library present in Java that is used to create dynamic web pages as well as rich web applications. JavaFX applications can run on various platforms, including mobile, web, and desktop. JavaFX codes can be run on various platforms. But the two most commonly used platforms for JavaFX are NetBeans and Eclipse.
What is Apache Struts 2?
Apache Struts 2 is used for developing Java EE web applications. It is an open-source web application framework. Apache Struts 1 extends and uses the Java Servlet API. The Apache Struts 2 is an enhancement over the Apache Struts 1.
How can we use the modelDriven Interceptor?
The modelDriven Interceptor is present in the ModelDriven Interface. Thus before we can start using the modelDriven Interceptor in our code, we need to implement the ModelDriven interface.
What is Apache-tomcat?
Apache-tomcat is an open source and free HTTP web server environment which is used to run and compile Java codes. Apache-tomcat is maintained and developed by an open group of developers.
How can we download Apache-tomcat, and what is the default port Apache-tomcat?
We can download the latest stable version of Apache-tomcat from the official Apache-tomcat website. Also, the default port for Apache-tomcat is 8080. Users can validate the connectivity of the Apache-tomcat server by going to https://localhost:8080.