struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nils-Helge Garli Hegvik <nil...@gmail.com>
Subject Re: how to get productID from the url into action class.
Date Thu, 28 May 2009 10:37:06 GMT
I think the mailing list is messing with the order of messages....

However,

Documentation and code snippets can be found here:
http://struts.apache.org/2.1.6/docs/bootstrap.html

Basically, map your action in struts.xml, add the appropriate
getter/setter in your action, and it should automagically be set from
the url to your action. If that doesn't work, then there's something
wrong with your setup.

Nils-H


On Thu, May 28, 2009 at 12:33 PM, Prashant Singh
<prashantk.singh@yahoo.co.in> wrote:
> Hi Nils,
>
> can you please illustrate with a code snippet.
>
> Thanks,
> Prashant
>
> --- On Thu, 28/5/09, Nils-Helge Garli Hegvik <nilsga@gmail.com> wrote:
>
>
> From: Nils-Helge Garli Hegvik <nilsga@gmail.com>
> Subject: Re: how to get productID from the url into action class.
> To: "Struts Users Mailing List" <user@struts.apache.org>
> Date: Thursday, 28 May, 2009, 3:19 PM
>
>
> Um... What exactly are you trying to do here? Do you just need the
> product id from the url string, or are you trying to fetch something
> from a different backend? If you just need it from the string, just
> use indexOf and substring.
>
> Nils-H
>
> On Thu, May 28, 2009 at 11:41 AM, Prashant Singh
> <prashantk.singh@yahoo.co.in> wrote:
>>
>>
>>
>>
>> Hi dear,
>>
>> I have been trying to read the productID from the url but it goes into an infinite
loop at point 1. I am posting the code below. Please help.
>>
>>
>>
>>
>>
>>
>>  package net.astralpharma;
>> import java.io.*;
>> import com.opensymphony.xwork2.ActionContext;
>> import com.opensymphony.xwork2.ActionSupport;
>> import org.apache.struts2.ServletActionContext;
>>
>> import java.io.BufferedReader;
>> import java.io.IOException;
>> import java.net.URL;
>> import java.net.URLConnection;
>> import java.sql.*;
>> import java.util.*;
>>
>> import net.astralpharma.Connect;
>> import net.astralpharma.Product;
>> import net.astralpharma.Productdisplay;
>>
>> public class Showprodsuppliers extends ActionSupport
>>     {
>>     private String productID;
>>     private String productName;
>>     List<Supplier> supplierName = new ArrayList<Supplier>();
>>
>>
>>     public String execute() throws Exception{
>>         StringBuffer sb = new StringBuffer("http://localhost:8080/Astralpharma1/astralpharma/astralpharma/showprodsuppliers.action?productID=1");
>>
>>         URL url = new URL(sb.toString());
>>         URLConnection urlConn = url.openConnection();
>>         urlConn.setUseCaches(true);
>>         urlConn.setDoOutput(true);
>>         urlConn.setDoInput(true);
>>         System.out.println("reached point 1");
>>
>>         OutputStreamWriter osr = new OutputStreamWriter(urlConn.getOutputStream());
>>
>>         BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
>>
>>         String tempStr;
>>         StringBuffer buffer = new StringBuffer();
>>         System.out.println("Reached point 2");
>>
>>         while((tempStr = br.readLine())!= null)
>>             {
>>                 buffer.append(tempStr + "<br>\n");
>>
>>             }
>>         br.close();
>>
>>         StringTokenizer st = new StringTokenizer(buffer.toString());
>>         ArrayList<String> output = new ArrayList<String>();
>>         System.out.println("reached point 3");
>>         while(st.hasMoreTokens())
>>         {
>>             String server = st.nextToken();
>>             try{
>>                 if(st.nextToken().contentEquals("productID"));
>>                 {
>>                     productID=st.nextToken().valueOf("productID");
>>                     System.out.println("Value of productID" + productID);
>>                 }
>>             }
>>             catch(Exception e)
>>             {
>>                 System.out.println("Exception" + e.getMessage());
>>             }
>>         }
>>         System.out.println("Reached point 4");
>>
>>             Connect connect = new Connect();
>>             Connection con = connect.useConnection();
>>             String query="Select suppliername from supplier_proddetails where productid=?";
>>             PreparedStatement stat = con.prepareStatement(query);
>>             stat.setString(1, productID);
>>             System.out.println(stat);
>>             ResultSet rs = stat.executeQuery();
>>             System.out.println("rs"+ rs);
>>             while(rs.next())
>>             {
>>                 System.out.println("inside while");
>>                 supplierName.add(new Supplier((rs.getString("supplierName"))));
>>                 System.out.println("item added successfully");
>>             }
>>         return "success";
>>         }
>>
>>         public List<Supplier> getSupplierName() {
>>             return  supplierName;
>>         }
>>
>>         public void setSupplierName(List<Supplier> supplierName) {
>>             this.supplierName = supplierName;
>>         }
>>
>>         public String  getProductID() {
>>             return  productID;
>>         }
>>
>>         public void setProductID(String productID ) {
>>             this.productID  = productID ;
>>         }
>>
>>
>>         public String getProductName() {
>>             return productName;
>>         }
>>
>>         public void setProductName(String productName) {
>>             this.productName = productName;
>>         }
>>
>> }
>>
>>
>>
>> My front end jsp is as follows:
>>
>> <%@ taglib prefix="s" uri="/struts-tags" %>
>>
>> <html>
>> <head>
>> <title>Product Handled</title>
>> <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
>> type="text/css"/>
>> </head>
>>
>> <body>
>> <table class="productTable">
>>
>>
>>
>> <tr><td>This Page Gives the List of Products handled by Astral Pharmaceuticals</td></tr>
>>
>> <tr><td>Products Handled</td></tr>
>>
>> <s:iterator value="productDetail" >
>> <tr><td><a href="showprodsuppliers.action?productID=<s:property
value="productID"/>">
>> <s:property value="productName"/>
>> </a></td></tr>
>> </s:iterator>
>>
>> </table>
>>
>>
>> </body>
>> </html>
>>
>> The url i am generating is as follows:
>> http://localhost:8080/Astralpharma1/astralpharma/astralpharma/showprodsuppliers.action?productID=1
>> I just need to get the productID from the url.
>>
>> Sincerely,
>> Prashant Singh
>>
>>
>>
>>
>>      Explore and discover exciting holidays and getaways with Yahoo! India Travel
http://in.travel.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>
>      Explore and discover exciting holidays and getaways with Yahoo! India Travel
http://in.travel.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message