package com.dineshonjava.struts2.login; import com.opensymphony.xwork2.ActionSupport; /** * @author Dinesh Rajput * */ @SuppressWarnings("serial") public class LoginAction extends ActionSupport{ private String username; private String password; private String userid; public String execute(){ String x = null; x = x.substring(0); return SUCCESS; } 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 getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } }Let us keep the content of Login.jsp as follows:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Struts 2 - Login Application | dineshonjava.com</title> </head> <body> <h2>Struts 2 - Login Application</h2> <font color="red"><s:actionerror /></font> <s:form action="welcome.action" method="post"> <s:textfield name="username" key="label.username" size="20" /> <s:password name="password" key="label.password" size="20" /> <s:submit method="execute" key="label.login" align="center" /> </s:form> </body> </html>
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome</title> </head> <body> <h2>Hello Welcome , <s:property value="username" />...! Dineshonjava.com</h2> </body> </html>
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="myapp" /> <package name="default" extends="struts-default" namespace="/"> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> </head> <body> This is my custom error page <form> <input type="button" value="back" onclick="history.back()"> </form> </body> </html>
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="myapp" /> <package name="default" extends="struts-default" namespace="/"> <global-results> <result name="myresult">/globalresult.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="myresult" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <action name="welcome" class="com.dineshonjava.struts2.login.LoginAction"> <result name="success">/Welcome.jsp</result> <result name="error">/Login.jsp</result> </action> </package> </struts>
Labels: struts2