This example explains about how to use the annotations in Struts
1) Create the blow java files under src folder of a web application with some package name.
LoginAction.java
UserModel.java
2) Create login.jsp, success and failure jsp pages under WebContent folder
3) In Annotation, the struts.xml does not required. All the configurations will be done at the action class.
LoginAction.java
package com.batch4;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction {
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;
}
@Action(value="/login",results={@Result(name="success",location="/login_success.jsp"),
@Result(name="failure",location="/login_failure.jsp")})
public String execute(){
System.out.println("UserName = "+userName);
System.out.println("Password = "+password);
if(userName.equals(password)){
//message = "UserName and password are valid";
return "success";
}else{
//message = "UserName and password are in-valid";
return "failure";
}
}
}
1) Create the blow java files under src folder of a web application with some package name.
LoginAction.java
UserModel.java
2) Create login.jsp, success and failure jsp pages under WebContent folder
3) In Annotation, the struts.xml does not required. All the configurations will be done at the action class.
LoginAction.java
package com.batch4;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction {
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;
}
@Action(value="/login",results={@Result(name="success",location="/login_success.jsp"),
@Result(name="failure",location="/login_failure.jsp")})
public String execute(){
System.out.println("UserName = "+userName);
System.out.println("Password = "+password);
if(userName.equals(password)){
//message = "UserName and password are valid";
return "success";
}else{
//message = "UserName and password are in-valid";
return "failure";
}
}
}
UserModel.java
package com.batch4;
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", "", "");
}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;
}
}
login.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> Welcome to STruts2 with annotations demo</h2>
<struts2:form action="login">
<struts2:textfield name="userName" label="LoginName"/>
<struts2:password name="password" label="Password"/>
<struts2:submit value="Login"/>
</struts2:form>
</body>
</html>
login_failed.jsp
<html>
<body>
<h2> UserName/password is invalid</h2>
</body>
</html>
login_success.jsp
<%@ taglib uri="/struts-tags" prefix="struts2"%>
<html>
<body>
<h2> user is successfully logged in</h2>
<struts2:property value="userName"/><br>
<struts2:property value="password"/><br>
</body>
</html>