1

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" extends="struts-default">

        <action name="getLogin" class="login.action.LoginAction"  
            method="login">
            <result name="success">/Profile.jsp</result>
            <result name="input">/LoginError.jsp</result>
        </action>

        <action name="getRegistered" class="login.action.LoginAction"
            method="register">
            <result name="success">/Success.jsp</result>
            <result name="input">/login.jsp</result>
        </action>

    </package>
</struts>

I'm trying to map two Action in this XML to two different methods in Action class

LoginAction.java:

package login.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import login.service.LoginDao;
import login.service.RegisterDao;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware {

    private String username;
    private String password;
    private HttpServletRequest httpServletRequest;
    private String firstname;
    private String lastname;
    private String regpassword;
    private String conpassword;
    private String regemail;
    private String conemail;
    private String phone;
    
    public String register(){
        RegisterDao rdao = new RegisterDao();
        String registered = rdao.registerdao(firstname,lastname,regpassword,conpassword,regemail,conemail,phone);
        if(registered == "TRUE"){
            return SUCCESS;
        }
        
        return INPUT;
    }
    
    
    public String login() {
        System.out.println("user name::: "+username);
        httpServletRequest.getSession().setAttribute("key", username);
        httpServletRequest.getSession().setAttribute("key", password);
        LoginDao db = new LoginDao();
        Boolean validate = db.loginresult(username, password);
        if (validate == true) {
            return SUCCESS;

        } else {
            return INPUT;
        }
    }
    
    public HttpServletRequest getHttpServletRequest() {
        return httpServletRequest;
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getRegpassword() {
        return regpassword;
    }

    public void setRegpassword(String regpassword) {
        this.regpassword = regpassword;
    }

    public String getConpassword() {
        return conpassword;
    }

    public void setConpassword(String conpassword) {
        this.conpassword = conpassword;
    }

    public String getRegemail() {
        return regemail;
    }

    public void setRegemail(String regemail) {
        this.regemail = regemail;
    }

    public String getConemail() {
        return conemail;
    }

    public void setConemail(String conemail) {
        this.conemail = conemail;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;

    }


    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;
    }

    @Override
    public void setServletResponse(HttpServletResponse arg0) {
        // TODO Auto-generated method stub

    }
    
    

}

Action Class with login and register methods:

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
    <center>
        <%@ taglib uri="/struts-tags" prefix="s"%>
        <s:form action="getLogin.action" method="post" validate="true">
            <s:textfield label="Name"  key="username"/>
            <s:password label="Password" key="password"/>
            <s:checkbox name="Rememberme" label="Remember Me" value="yes"
                checked="checked"/>
            <s:submit value="Sign in"/>
        </s:form>
    </center>
</body>
</html>

Login.jsp which is redirecting to the login method in Action class. Earlier it was working fine. Now this action gives trouble:

Registration.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
    <center>
        <%@ taglib uri="/struts-tags" prefix="s"%>
        <s:form action="getRegistered.action" method="post" validate="true">
            <div>
                <table>
                    <s:textfield label="First Name" key="firstname" />
                    <s:textfield label="Last Name" key="lastname" />
                    <s:password label="Create your password" key="regpassword" />
                    <s:password label="Confirm your password" key="conpassword" />
                    <s:textfield label="Email" key="regemail1" />
                    <s:textfield label="Re-Type Email" key="conemail" />
                    <s:textfield label="Phone" key="phone" />   
                    <tr>
                    <td><s:submit value="Register" theme="simple"/></td>
                    <td><s:submit value="Cancel" theme="simple" onclick="document.forms[0].action='login.jsp';" /></td>
                    </tr>
                </table>
            </div>
        </s:form>
    </center>
</body>
</html>

Registration.jsp redirecting to the getRegistered action:

LoginAction-Validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"  
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <!-- Login page validation Starts -->
    <field name="username">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>User Name is mandatory</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <param name="trim">true</param>
            <message>User Name must be between 5 to 10 characters long</message>
        </field-validator>
    </field>

    <field name="password">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Enter your password</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <param name="trim">true</param>
            <message>Password must be between 5 to 10 characters long</message>
        </field-validator>
        <field-validator type="regex">
            <param name="expression">((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{5,10})</param>
            <message>Password must </message>
        </field-validator>
    </field>
    <!-- Login page validation ends -->
</validators>

Validation xml for the login user name and the password..

When I run this code, it is always returning the "INPUT" string and redirecting to LoginError.jsp in the case of login action and login.jsp in the case of register action. Please suggest how I can resolve this issue without using validation.excludeMethods

LoginAction-getRegistered-validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"  
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<!-- Registration validation Starts -->
    <field name="firstname">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Firstname is mandatory</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <param name="trim">true</param>
            <message>First Name must be between 5 to 10 characters long</message>
        </field-validator>
    </field>
    <field name="lastname">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Lastname is mandatory</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <param name="trim">true</param>
            <message>Last Name must be between 5 to 10 characters long</message>
        </field-validator>
    </field>
    <!-- Registration validation ends -->
</validators>

I have made validation for the first and the last name

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3747665
  • 43
  • 1
  • 5

2 Answers2

0

Remove .action extension from action name in <s:form>, and make sure your struts.xml is copied under WEB-INF/classes when deploying.

<s:form action="getRegistered" method="post" validate="true">

An cleaner approach would be to use <s:url> and reference the created URL in <s:form>.

<s:url var="actionUrl" action="getRegistered" namespace="" />
<s:form action="%{actionUrl}" method="post" validate="true">
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I changed the form with the as suggested ... And my struts.xml is in WEB-INF/Classes folder.. Still it is redirecting to the loginError.jsp and login.jsp – user3747665 Jun 18 '14 at 15:33
0

In your validation file which is per class, so it validates all actions/methods in this class that are intercepted by validation interceptor. You should separate validation logic for the first action getLogin like LoginAction-getLogin-validation.xml and define another validation file for getRegistered action like LoginAction-getRegistered-validation.xml. They would be processed per action. Also regex validator has a parameter regex instead of expression.

<field-validator type="regex">
  <param name="regex"><![CDATA[((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{5,10})]]></param>
  <message>Password must </message>
</field-validator>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I created validation xml for register and login methods. Still i face issue for redirecting in the case of register method . It is always redirecting to the /login.jsp – user3747665 Jun 18 '14 at 15:57
  • You didn't post field validators for register. I have suggested you a solution that you can use to solve your problem. I'm not intended to solve problems with unclear and incomplete questions. – Roman C Jun 18 '14 at 16:05
  • Sorry for that , i have just added the LoginAction-getRegistered-validation.xml with the question – user3747665 Jun 18 '14 at 16:19
  • Did you rename the first file? – Roman C Jun 18 '14 at 16:34
  • I removed the first file . I have LoginAction-getRegistered-validation.xml and LoginAction-getLogin-validation.xml now in my project – user3747665 Jun 18 '14 at 16:36
  • You should not return `INPUT` result, this result returns `validation` interceptor when validation errors. Are you sure the action method is called? – Roman C Jun 18 '14 at 18:28
  • I identified the mistake : I was just giving the first name value while i have made validations for first name and last name .. That's why when i input only first name its redirecting to input scenario .. After i give the first name and last name in the input , its redirecting properly. The validation i have made for the register action just doesn't show any error message in the UI when i dont input the last name . Thanks for your assistance Roman – user3747665 Jun 19 '14 at 04:59