How to configure the struts2 web application -
Step 1 -
web.xml
add filter dispatcher
<filter>
<filter-name>struts2</filter- name>
<filter-class>
org.apache.struts2.dispatcher. FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter- name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Step 2 -
add 5 jar files which required in struts2.x
- commons-logging-1.1.jar
- freemarker-2.3.8.jar
- ognl-2.6.11.jar
- struts2-core-2.0.6.jar
- xwork-2.0.1.jar
Step 3 -
add resource folder in below of src folder.
- create struts.xml file in this resource folder.
- create ApplicationResource.properties file in this resource folder
Step 4 -
In struts.xml
add
<!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.enable. DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n. resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.jeet.struts2. LoginAction" method="authenticate">
<result name="success">Welcome.jsp</ result>
<result name="error">Login.jsp</ result>
</action>
</package>
</struts>
- If you want to include ApplicationResource properties file so you have to add this file path in set in constant tag.
- In case of struts2 by default execute method call of action class.
but if you want to call your own method so you have to set constant
name="struts.enable.
DynamicMethodInvocation" value="true" if you set value="false" means you want to call by default execute method. - In package you can create multiple action and also call a new method for every action. mention action class name and method name.set view page according to result means if success show different page and if fail show another page.
Step 4 -
create view (JSP) page -
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application</title>
</head>
<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="authenticate" key="label.login" align="center" />
</s:form>
</body>
</html>
- set taglib with prefix s and uri struts-tags
- if you want to show server side error so you have to add <s:actionerror />
- if form tag pass action with name.action.
- if you want label value coming from .properties file so in key parameter you can write.
- At the time of submit you also can specify which method you want to call on click of that button.
Step 5 -
Action Class
import com.opensymphony.xwork2. ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String authenticate() {
if (this.username.equals("admin") && this.password.equals(" admin123")) {
return "success";
} else {
addActionError(getText("error. login"));
return "error";
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- If you want your class action class with struts 2 so extends ActionSupport class.
- Either override execute() method or if you want to create own method so you can create.
- If you want to send server side error in view page so call addActionError() method and pass your message either directly or through properties file.
Comments
Post a Comment