0

I am trying to view data from databases on JSP, so now I come up with idea to use it without scriptlets or Java code inside. I have learned the modelDriven interceptor and some other interceptors to use in struts.xml, but I don't know how to implement it.

Beantest.java:

public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getBook()
    {
        return Book;
    }

    public void setBook(String book)
    {
        Book = book;
    }

    public String getAuthor()
    {
        return Author;
    }

    public void setAuthor(String author)
    {
        Author = author;
    }

    public String getAvailbleqty()
    {
        return Availbleqty;
    }

    public void setAvailbleqty(String availbleqty)
    {
        Availbleqty = availbleqty;
    }

    public String getCategory()
    {
        return Category;
    }

    public void setCategory(String category)
    {
        Category = category;
    }

DataAction.java:

public List<Beantest> viewbook()
{

    List<Beantest> al=new ArrayList<Beantest>();
    Beantest bt = new Beantest();
    try
    {
        String sql = "select * from Bookavaible";
        Statement stmt;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next())
        {
            bt.setId(rs.getString("id"));
            bt.setBook(rs.getString("Book"));
            bt.setAuthor(rs.getString("Author"));
            bt.setAvailbleqty(rs.getString("Availbleqty"));
            bt.setCategory(rs.getString("Category"));
            al.add(bt);
        }
    } 
    catch (SQLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return al;

}

Actiontest.java:

 public class ActionTest {
    Beantest bt;
    
    private List<Beantest> beans;

    public String viewbookaction()
    {
        DataAction da = new DataAction();
        beans = da.viewbook();
        return ActionSupport.SUCCESS;
    }

    public List<Beantest> getBeans()
    {
        return beans;
    }

Bookview.jsp:

<s:action name="VBA">
        <td>id:</td>

        <td>Book:</td>

        <td>Author:</td>

        <td>Availbleqty:</td>

        <td>Category:</td>

</s:action>

web.xml:

  <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <welcome-file-list>
        <welcome-file>BookView.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

strut2.xml:

<package name="a" namespace="/">
    <action name="VBA" class="Action.ActionTest" method="viewbookaction">
        <result name="success">/BookView.jsp</result>
    </action>
</package>
5
  • you can use struts2 tags to display data (eg property tag ) by binding data from action Commented May 16, 2014 at 6:37
  • thank for u reply if u have time kindly provide solution Commented May 16, 2014 at 12:51
  • 1
    Please look at essentially any S2 tutorial. Also, do you really need to use the <s:action> tag here? I don't see why. Commented May 16, 2014 at 13:04
  • k fine i am learning only but i dono how to pass value from xml to jsp without using javacode Commented May 16, 2014 at 13:53
  • Exact solution refer tech.learnerandtutor.com/… Commented May 17, 2014 at 2:02

2 Answers 2

3

Don't use <s:action> tag in JSP, until you know how and why to do that. To display values in JSP you are not needed it at all. You should use struts tags to access action bean properties. For example you can print values to the JSP out using <s:property> tag. For example

<s:iterator value="beans">    
    id: <s:property value="id"/><br>    
    Book: <s:property value="book"/><br>    
    Author: <s:property value="author"/><br>
    Availbleqty: <s:property value="availbleqty"/><br>    
    Category: <s:property value="category"/><br>    
<s:/iterator>

The FilterDispatcher is deprecated and you should replace it with StrutsPrepareAndExecuteFilter. See web.xml docs. Also see this answer for Need suggestions regarding project implementation in Struts and Hibernate.

Sign up to request clarification or add additional context in comments.

9 Comments

fine sir i will follow but it can't show a single value in jsp page?? i dont now why??
The value in JSP will be available after you call an action and return result with this JSP.
ya sir i called a Action with method name method="viewbookaction" and result page is "Bookview jsp"
What is the size of list returned by viewbook() method? And remove ModelDriven from your action class, because I don't see it in the question.
while i am printing list it return value like "list[Beanclass.Beantest@1e87539]"
|
1

Change your web.xml and stuts.xml as below.

Struts.xml

<struts>
<constant name="struts.devMode" value="true" />
<package name="a" namespace="/" extends="struts-default">
    <action name="" class="Action.ActionTest" method="viewbookaction">
        <result name="success">/BookView.jsp</result>
    </action>
</package>

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

That's all. Simply run.

1 Comment

thanks for u r effort it not showing a Result i will check my side and will Update here??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.