*
The moose likes Struts and the fly likes how do i use html:select to forward to different pages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how do i use html:select to forward to different pages?" Watch "how do i use html:select to forward to different pages?" New topic
Author

how do i use html:select to forward to different pages?

pratik mehta
Greenhorn

Joined: Jan 28, 2009
Posts: 3

Hi , i need to know that, what do i need to edit in the action class or if here to route my page according to the option i select from the htmlption.
i am a newbie so little help will be required.
Do i need to edit anythin in the action class..? or do ihave to do it here??






JSP PAGE


<%@ 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">

<%@ taglib uri="WEB-INF/tlds/struts-html" prefix="html"%>
<%@ taglib uri="WEB-INF/tlds/struts-logic" prefix="logic"%>
<%@ taglib uri="WEB-INF/tlds/struts-bean" prefix="bean"%>

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1 align="center"><u>Table 1</u></h1>
<html:form action="/hello" method="post" >
<table id="cs1" width="" border="0" align="center" bgcolor="white" >
<tr>
<th align="center">
Name<html:text property="Name" />
</th>
</tr>
<tr>
<th align="center">From Date <html:text property="fromDate" /></th>
</tr>
<tr>
<th align="center" >
To Date <html:text property="toDate" />
</th>
</tr>
<tr>
<th align="center" >
Type<html:select property="Type" size="1" >
<htmlption value="0">A </htmlption>
<htmlption value="1">B</htmlption>
<htmlption value="2">C</htmlption>
</html:select>
</th>
</tr>
<tr>
<th align="center">
2Name<html:select property="2Name" size="1" >
<htmlption value="0"> J2</htmlption>
<htmlption value="1"> J1</htmlption>
<htmlption value="2"> J3</htmlption>
</html:select>
</th>
</tr>

<tr></tr>
<tr>
<th>
<html:submit />

</th>
</tr>



</table>
</html:form>
</body>
</html>




ACTION CLASS


package ui.web.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class CommonServicesAction extends Action{public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
{
return mapping.findForward("success3");
}}




Struts config

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>


<!-- ========== Form Bean Definitions ============ -->
<form-beans>

<form-bean name="HelloForm" type="ui.web.struts.action.HelloForm" />


</form-beans>







<!-- ========== Action Mapping Definitions ======== -->
<action-mappings>
<action
path="/hello"
type="ui.web.struts.action.helloAction"
name="HelloForm"
scope="session"
validate="true"
>
<forward name="success3" path="/hellohello.jsp" />
</action>
</action-mappings>

</struts-config>




Action FORM

import org.apache.struts.action.*;
public class CommonServicesForm extends ActionForm{
private String Name=null;
private Object fromDate=null;
private Object toDate=null;
private String Type;
private String 2Name;
public void setFromDate(Object fromDate) {
this.fromDate = fromDate;
}
public Object getFromDate() {
return this.fromDate;
}
public void setName(String Name) {
this.Name = Name;
}
public String getName() {
return this.Name;
}
public void setToDate(Object toDate) {
this.toDate = toDate;
}
public Object getToDate() {
return toDate;
}
public void setType(String Type) {
this.Type = Type;
}
public String getType() {
return Type;
}
public void set2Name(String 2Name) {
this.2Name = 2Name;
}
public String get2Name() {
return 2Name;
}
}

Praveen Sangolli
Greenhorn

Joined: May 22, 2008
Posts: 28
Hi Pratik,

You can use javascript to forward to a new page. All that you need to do is that onchange call a javascript function that forwards the request to a different page. eg

<script>
function forward(val){
window.location.href=val
}
</script>

<select name="hjh" onchange="forward(this.value)">
<option value="index.html" >M</option>
<option value="menu.html" >N</option>
</select>





rahulash sharma
Greenhorn

Joined: Jan 05, 2009
Posts: 15
You can use requestDispatcher class. Retrieve value and forward to page according to you .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do you mean you want to submit the form and select the ActionForward based on the value in the dropdown? Just return a different ActionForward based on form value. ActionForwards can also be created from URLs.

When you post code please (a) disable smilies, and (b) put it in code tags:

/

so the code remains formatted:


Nyanas Kandhan
Ranch Hand

Joined: Jan 27, 2009
Posts: 35
You can do this using js or you can forward it to a jsp page and based on the value selected you can forward to different jsp pages using jsp:forward.

regards
Struts Developer

regards,
Nyanaskandhan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do i use html:select to forward to different pages?
 
Similar Threads
strange error with respect to web.xml file
javax.servlet.jsp.JspException: No getter method for property: "id" of bean: "Login.LoginForm"
strut exception
java.lang.IllegalArgumentException: argument type mismatch
how do i use checkbox as an active/inactive button for a form?