shakeer_m
asked on
Multiple buttons in login.jsp
Hey all,
I've been trying to learn struts, installed netbeans with their excellent tutorials, was able to create a simple login application. But now I need to add a Sign Up/Register button along with login button. Tried adding a href link to my register.jsp but does not work, please help.
Thanks guys.
I've been trying to learn struts, installed netbeans with their excellent tutorials, was able to create a simple login application. But now I need to add a Sign Up/Register button along with login button. Tried adding a href link to my register.jsp but does not work, please help.
Thanks guys.
I'm not sure why you need a hyperlink, when you have button...
Create your buttons like this..
<html:button property="dispatch" onclick="submitMyForm()"> Submit </html:button>
<html:button property="dispatch" onclick="register()"> Sign Up/Register </html:button>
and javascript methods like this...
submitMyForm(){
document.forms[0].action= "/yourContextPath/actionPa thForSubmi t";
document.forms[0].submit() ;
}
register(){
document.forms[0].action= "/yourContextPath/actionPa thForRegis tert";
document.forms[0].submit() ;
}
let me know how it helps!!
Create your buttons like this..
<html:button property="dispatch" onclick="submitMyForm()"> Submit </html:button>
<html:button property="dispatch" onclick="register()"> Sign Up/Register </html:button>
and javascript methods like this...
submitMyForm(){
document.forms[0].action= "/yourContextPath/actionPa
document.forms[0].submit()
}
register(){
document.forms[0].action= "/yourContextPath/actionPa
document.forms[0].submit()
}
let me know how it helps!!
ASKER
Thanks for your valuable comments, attached below is with your suggestions please let know whats not right
login.jsp below
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Intro Page</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1>Welcome</h1>
<html:form action="/login">
<table width="354" height="158" border="0">
<tbody>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
</td>
</tr>
<tr>
<td width="95">User ID : </td>
<td width="249"><html:text property="userID" /></td>
</tr>
<tr>
<td>Password : </td>
<td><html:text property="password"/></td>
</tr>
<tr>
</tr>
</tbody>
</table>
<p>
<input type="submit" value="Submit">
</p>
<a link href="WEB-INF/register.jsp " onclick='register()' >Click to register</a>
<script type="text/javascript">
function register()
{
document.forms[0].action = "RegisterAction";
document.forms[0].submit;
}
</script>
</html:form>
</body>
</html>
from strtuts-config
<action-mappings>
<action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.Log inAction" validate="false">
<forward name="success" path="/WEB-INF/success.jsp "/>
<forward name="failure" path="/login.jsp"/>
</action>
<action name="RegisterForm" path="/register" scope="request" type="com.myapp.struts.Reg isterActio n" validate="false">
<forward name="success" path="/WEB-INF/registerSuc cess.jsp"/ >
<forward name="failure" path="/WEB-INF/register.js p"/>
</action>
</action-mappings>
And Gurvinder whats the better way to handle this using MVC? dont want to struggle if there is a better way
Thanks.
login.jsp below
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Intro Page</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1>Welcome</h1>
<html:form action="/login">
<table width="354" height="158" border="0">
<tbody>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
</td>
</tr>
<tr>
<td width="95">User ID : </td>
<td width="249"><html:text property="userID" /></td>
</tr>
<tr>
<td>Password : </td>
<td><html:text property="password"/></td>
</tr>
<tr>
</tr>
</tbody>
</table>
<p>
<input type="submit" value="Submit">
</p>
<a link href="WEB-INF/register.jsp
<script type="text/javascript">
function register()
{
document.forms[0].action = "RegisterAction";
document.forms[0].submit;
}
</script>
</html:form>
</body>
</html>
from strtuts-config
<action-mappings>
<action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.Log
<forward name="success" path="/WEB-INF/success.jsp
<forward name="failure" path="/login.jsp"/>
</action>
<action name="RegisterForm" path="/register" scope="request" type="com.myapp.struts.Reg
<forward name="success" path="/WEB-INF/registerSuc
<forward name="failure" path="/WEB-INF/register.js
</action>
</action-mappings>
And Gurvinder whats the better way to handle this using MVC? dont want to struggle if there is a better way
Thanks.
<<<a link href="WEB-INF/register.jsp " onclick='register()' >Click to register</a>>>
you should not provide jsp link in the href
<a link href="#" onclick='register()' >Click to register</a>
you should not provide jsp link in the href
<a link href="#" onclick='register()' >Click to register</a>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
changed to <a link href="#" onclick='register()' >Click to register</a>
but link does not work, attached are LoginAction and RegisterAction, please review.
Thanks.
but link does not work, attached are LoginAction and RegisterAction, please review.
Thanks.
LoginAction.java below
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// extract user data
LoginForm formBean = (LoginForm) form;
String userID = formBean.getUserID();
String password = formBean.getPassword();
// perform validation
if ((userID == null) || // name parameter does not exist
password == null || // email parameter does not exist
userID.equals("")) { // name parameter is empty
// password.indexOf("@") == -1)
// email lacks '@'
formBean.setError();
return mapping.findForward(FAILURE);
}
return mapping.findForward(SUCCESS);
}
}
RegisterAction.java below
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class RegisterAction extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// extract user data
RegisterForm formBean = (RegisterForm) form;
String firstName = formBean.getFirstName();
String password = formBean.getPassword();
// perform validation
if ((firstName == null) || // name parameter does not exist
password == null || // email parameter does not exist
firstName.equals("")) { // name parameter is empty
// password.indexOf("@") == -1)
// email lacks '@'
formBean.setError();
return mapping.findForward(FAILURE);
}
return mapping.findForward(SUCCESS);
}
}
can you try giving
<%@ taglib uri="/WEB-INF/struts-html. tld" prefix="html"%>
<html:link page="/success.do">Call the Success page</html:link>
<%@ taglib uri="/WEB-INF/struts-html.
<html:link page="/success.do">Call the Success page</html:link>
ASKER
Did not work,
org.apache.jasper.JasperEx ception: /login.jsp(8,55) Attempt to redefine the prefix html to /WEB-INF/struts-html.tld, when it was already defined as http://struts.apache.org/tags-html in the current scope.
removed the taglib still the link does not work.
org.apache.jasper.JasperEx
removed the taglib still the link does not work.
there is no need to copy
<%@ taglib uri="/WEB-INF/struts-html. tld" prefix="html"%>
in your code
if you already have
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
in the original code that you wrote
<%@ taglib uri="/WEB-INF/struts-html.
in your code
if you already have
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
in the original code that you wrote
<<<<<action name="RegisterForm" path="/register" scope="request"
check that !!
check that !!
ASKER
struts mapping for Login is as attached below and hence used the same format for register page, also tried running with change like this,
<action name="RegisterAction" path="/register" scope="request" type="com.myapp.struts.Reg isterActio n" validate="false">
still does not work, thanks for all the valuable comments, please guide me to the correct way of adding another button to a jsp page that forwards to a different page which will take in all the registration information.
<action name="RegisterAction" path="/register" scope="request" type="com.myapp.struts.Reg
still does not work, thanks for all the valuable comments, please guide me to the correct way of adding another button to a jsp page that forwards to a different page which will take in all the registration information.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="RegisterForm" type="com.myapp.struts.RegisterForm"/>
<form-bean name="LoginForm" type="com.myapp.struts.LoginForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<action-mappings>
<action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false">
<forward name="success" path="/WEB-INF/success.jsp"/>
<forward name="failure" path="/login.jsp"/>
</action>
<action name="RegisterForm" path="/register" scope="request" type="com.myapp.struts.RegisterAction" validate="false">
<forward name="success" path="/WEB-INF/registerSuccess.jsp"/>
<forward name="failure" path="/WEB-INF/register.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
ASKER
From a comment earlier, I'm guessing the correct way is to use the LoginAction class to forward to the page, adding a third mapping along with 'success' and 'failure' as 'register' if the button is clicked, is this right?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to give more details, as to what is the code that you have written, what is the issue you are facing for that.
for signup
1) create a action class to handle the signup actions, SignupAction
2) make an entry in the struts-config.xml
3) invoke the action class from the JSP by
in JSP
<a href="#" onclick='doSignUp()' >
Javascript
function doSignUp()
{
document.forms[0].action = "SignupAction.do";
document.forms[0].submit;
}
4) SignupAction should then forward the control to the register.jsp
Since you are learning Struts, you should try to use the controller (MVC), and not try to invoke a JSP from a JSP.
Thanks