如何在 JSP 上查看数据库中的数据,而无需在 Struts 2 中使用 scriptlet 或 Java 代码?

问题描述 投票:0回答:2

我正在尝试在 JSP 上查看数据库中的数据,所以现在我想出了在 JSP 中不使用 alscriptlet 或 Java 代码的想法。我已经学习了

modelDriven
拦截器和一些其他拦截器在
struts.xml
中使用,但我不知道如何实现它。

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>
java jsp model-view-controller struts2 struts-tags
2个回答
3
投票

不要在 JSP 中使用

<s:action>
标记,除非您知道如何以及为何这样做。要在 JSP 中显示值,您根本不需要它。您应该使用 struts 标签来访问操作 bean 属性。例如,您可以使用
<s:property>
标签将值打印到 JSP 输出。例如

<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>

FilterDispatcher
已弃用,您应该将其替换为
StrutsPrepareAndExecuteFilter
。请参阅
web.xml
文档。另请参阅此答案 需要有关 Struts 和 Hibernate 中项目实施的建议


1
投票

如下更改您的 web.xml 和 stuts.xml。

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>

仅此而已。只需运行即可。

© www.soinside.com 2019 - 2024. All rights reserved.