Struts 2 form with validation and XHTML CSS layout (Part 1)
Problem summary
I wanted to create a simple login form using Struts 2 for the framework. I wanted the form to do the following:- To be laid out using CSS;
- To validate the data the user entered;
- To make a decision about the data entered and either return the user to the login form with an error message or redirect them to a login success page.
Setting up the home page
Most web sites don’t have their full login page on their home page so I first wanted a home page that would link to the login page:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts 2 you - login example - index page</title>
<link rel="stylesheet" href="/struts2-login/css/css.css" type="text/css">
</head>
<body>
<h2>Struts 2 you - example login</h2>
<p>
<s:url id="urlLogin" action="login!input" namespace="/usr" />
<s:a href="%{urlLogin}">Login</s:a>
</p>
</body>
</html>
This is a very simple jsp page but it does have the
<package name="struts2-login" extends="struts-default" namespace="/usr">
<action name="login" class="struts2you.examplelogin.Login" method="input">
<result name="input">/usr/login.jsp</result>
</action>
</package>
When the user clicks on the link on the home page they are processed through the above action mapping. If you download the .war file associated with this tutorial and look at the struts2you.examplelogin.Login class you will see it has no method input(). This is because we have defined the method ‘input’ in the struts.xml file; there is a <result name=”input”> node. This means that all though the user is sent through the Struts 2 framework they do not actually hit our struts2you.examplelogin.Login class. This may seem odd, and it took me a while to work this out, but if we don’t do it this way the user will be processed through our action and be redirected to the login page with the action claiming that the user failed to input valid data. This means that if we do not hit the input method then the user will click the Go to the login page link and will see the login form, complete with the:
- You entered an invalid username;
- You entered an invalid password
validation errors. This is a little harsh as they haven’t been given a chance to get it right so far! By processing them through the login!input URL and defining the input result in the struts2login.xml file we can take them through the Struts framework and not tell them they have entered invalid login information as they have not hit our struts2you.examplelogin.Login class. I think this is quite complicated, but it is also necessary as all URL’s should be processed through the Struts framework, as just using a hyperlink to /usr/login.jsp would not trigger any other Struts processing on the page (if there was any). I hope that all makes sense! If not download the .war file for this tutorial and play around for a while until it does. It took me a good days work to work that bit out! If anyone knows a better way of doing it then please let me know.
19.02.2009 10:12 - Posted by doahh - Comments: 6 - Java

Comments:
Hello,
I think a couple of remarks are to be made regarding what you state here. :)
First of all,
! you have an input() method in your Login class.
Explanation: Your Login class extends BaseActionSupport, that you defined. Nothing strange here :). But this class further extends ActionSupport:
" public class BaseActionSupport extends ActionSupport { "
ActionSupport is a support class from the Struts2 framework. IT CONTAINS THE input() METHOD.
Second of all,
! the input() method returns the "input" String
Why "input" built in? : The convention is that when the form is no good, you are redirected to the INPUT page(with the bad form), so "input" is like a default word for this page. That's why they return this string exactly in the input() method.
Third of all,
! you don't define the method input() in the action declaration in the struts file (method="input")
Explain: I think now is clear that you don't define it there. You just point out the method struts is to use when you call that action. If you say there method="alabala" then struts tryes to find that method to execute for that action.
But how come it works?
You specify the action="login!input" that calls exactly the input() method from the class associated with login action (Login class). And Struts is able to find it as i said earlier, and go to the "input" result as commanded by it.
Conclusion:
either use method="input" and no !input
or
use !input and no need for method="input"
Am i right or am i right? ;) TRY IT
06.03.2009 03:22 - Posted by Bogdan - Permalink
Hello,
I think a couple of remarks are to be made regarding what you state here. :)
First of all,
! you have an input() method in your Login class.
Explanation: Your Login class extends BaseActionSupport, that you defined. Nothing strange here :). But this class further extends ActionSupport:
" public class BaseActionSupport extends ActionSupport { "
ActionSupport is a support class from the Struts2 framework. IT CONTAINS THE input() METHOD.
Second of all,
! the input() method returns the "input" String
Why "input" built in? : The convention is that when the form is no good, you are redirected to the INPUT page(with the bad form), so "input" is like a default word for this page. That's why they return this string exactly in the input() method.
06.03.2009 03:23 - Posted by Bogdan - Permalink
Third of all,
! you don't define the method input() in the action declaration in the struts file (method="input")
Explain: I think now is clear that you don't define it there. You just point out the method struts is to use when you call that action. If you say there method="alabala" then struts tryes to find that method to execute for that action.
But how come it works?
You specify the action="login!input" that calls exactly the input() method from the class associated with login action (Login class). And Struts is able to find it as i said earlier, and go to the "input" result as commanded by it.
Conclusion:
either use method="input" and no !input
or
use !input and no need for method="input"
Am i right or am i right? ;) TRY IT
06.03.2009 03:24 - Posted by Bogdan - Permalink
Well spotted, you are almost right. There is no need for the <em>method="input"</em> in the XML action definition. I do however find that that index.jsp page needs the <em>action="login!input"</em> or else when you enter the login page you are immediately told that your username and password are wrong. If that is the first time you have been to the page that seems a little harsh.
07.03.2009 06:20 - Posted by doahh - Permalink
I've tried using login!input but it keeps throwing an error. Its actually signup!input for me. When I call just "signup" it calls my Action (which has an input method. When I go to signup!input, I get:
There is no Action mapped for namespace / and action name signup!input.
Do I need to have a namespace for all my actions, for issues like this one?
14.09.2010 02:40 - Posted by Charles Godfrey - Permalink
how to use css with struts2?
10.01.2012 05:36 - Posted by pooja - Permalink