This example explains about writing the fist sample program in Struts 2.x
1) Create a web application "Struts2Example2" in Eclipse
2) Create a login.jsp, login_success.jsp and login_failed.jsp under WebContent Folder of your web application
3) Create a LoginAction.java class in src folder
4) Create a UserModel.Java class in src folder
5) create and constructs struts.xml in src folder
6) Finally modify the web.xml
The all the files and their code is given below.
login.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> Welcome to STruts2 demo</h2>
<struts2:form action="login.action">
<struts2:textfield name="userName" label="LoginName"/>
<struts2:password name="password" label="Password"/>
<struts2:submit value="Login"/>
</struts2:form>
</body>
</html>
login_success.jsp
<html>
<body>
<h2> user is successfully logged in</h2>
</body>
</html>
login_failed.jsp
<html>
<body>
<h2> UserName/password is invalid</h2>
</body>
</html>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String userName;
private String password;
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;
}
public String execute() throws Exception {
boolean result = UserModel.isUserNameAndPasswordExist(userName, password);
if(result){
return "success";
}else{
return "failure";
}
}
}
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>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="LoginAction">
<result name="success">/login_success.jsp</result>
<result name="failure">/login_failed.jsp</result>
</action>
</package>
</struts>
1) Create a web application "Struts2Example2" in Eclipse
2) Create a login.jsp, login_success.jsp and login_failed.jsp under WebContent Folder of your web application
3) Create a LoginAction.java class in src folder
4) Create a UserModel.Java class in src folder
5) create and constructs struts.xml in src folder
6) Finally modify the web.xml
The all the files and their code is given below.
login.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> Welcome to STruts2 demo</h2>
<struts2:form action="login.action">
<struts2:textfield name="userName" label="LoginName"/>
<struts2:password name="password" label="Password"/>
<struts2:submit value="Login"/>
</struts2:form>
</body>
</html>
login_success.jsp
<html>
<body>
<h2> user is successfully logged in</h2>
</body>
</html>
login_failed.jsp
<html>
<body>
<h2> UserName/password is invalid</h2>
</body>
</html>
LoginAction.java
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String userName;
private String password;
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;
}
public String execute() throws Exception {
boolean result = UserModel.isUserNameAndPasswordExist(userName, password);
if(result){
return "success";
}else{
return "failure";
}
}
}
UserModel.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class UserModel {
private static Connection con;
static {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "user", "passwd");
}catch(Exception e){
e.printStackTrace();
}
}
public static boolean isUserNameAndPasswordExist (
String userName,String password){
boolean result = false;
try{
String sql = "select * from user where password = ? and login_name = ?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(2, userName);
ps.setString(1, password);
ResultSet rs = ps.executeQuery();
if(rs.next() && rs.getInt(1)> 0){
result = true;
}
}catch(SQLException e){
e.printStackTrace();
}
return result;
}
}
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="LoginAction">
<result name="success">/login_success.jsp</result>
<result name="failure">/login_failed.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2Example2</display-name>
<filter>
<filter-name>Struts2Controller</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2Controller</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>