Java上将数据库中的数据以表单的形式输出给用户

2024-03-08

我最近开始学习Java。 我需要用 Java 编写一个 Web 应用程序,用户可以从下拉列表中的主页 html 页面上的表单中选择他所需的产品。产品列表存储在数据库的表中(使用 MySQL)。然后将所选产品写入“订单历史记录”表中。 如何将数据库中的数据输出到下拉列表中? 如何实现用户选择所需的产品? 我该如何开始?有人有一个小例子吗?


Vaadin

这是一个使用 Java 的完整工作示例Vaadin http://www.Vaadin.com/框架版本 8.5.2,用于创建一个运行数据库的 Web 应用程序H2数据库引擎 http://h2database.com/html/main.html跟踪产品列表(太阳系的 10 个行星)。 ANativeSelectVaadin 中的小部件由List of Product从 a 加载的对象product_该数据库中的表。每次用户点击Order按钮,订单被记录为一行order_ table.

这是数据库中两个表的简单 ERD 图。

用户首先从下拉列表中选择星球产品,然后单击“订购”按钮。

在此数据输入区域下方,您会看到一对Grid小部件作为数据库的后门。左边是产品列表,没有变化。右侧是所有订单的列表,每次用户使用“订单”按钮下订单时都会更新。

请注意,数据库位于内存中,而不是持久的,因为这只是一个小演示。因此,每次启动应用程序时,数据库都会从头开始重建。这order_每次运行时表都是空的。

最后,这绝不是生产就绪的代码,只是一个展示可能性的示例。刚才在拍摄屏幕截图时,我发现了一个与根本不选择产品相关的错误。这就是生活。

有关详细信息,请参阅以下部分Vaadin manual:

  • Button https://vaadin.com/docs/v8/framework/components/components-button.html– 按钮小部件概述。
  • 本地选择 https://vaadin.com/docs/v8/framework/components/components-nativeselect.html— 此下拉列表小部件的快速演示
  • 选择组件 https://vaadin.com/docs/v8/framework/components/components-selection.html#components.selection— 讨论小部件如何NativeSelect在瓦丁工作
  • Grid https://vaadin.com/docs/v8/framework/components/components-grid.html— 如何使用这个强大的数据网格小部件。

主应用程序类

基本上是样板文件,让 Vaadin 运行。重要的是中间的那对线,对于ProductPickerLayout.

package com.basilbourque.example;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of an HTML page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@Theme ( "mytheme" )
public class MyUI extends UI {

    @Override
    protected void init ( VaadinRequest vaadinRequest ) {
        final Layout layout = new ProductPickerLayout();
        this.setContent( layout );
    }

    @WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
    @VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
    public static class MyUIServlet extends VaadinServlet {
    }
}

Servlet 上下文监听器

对网络应用程序的启动和退出做出反应。

package com.basilbourque.example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.time.Instant;

@WebListener
public class MyServletContextListener implements ServletContextListener {
    MyDatabaseService databaseService;

    @Override
    public void contextInitialized ( ServletContextEvent servletContextEvent ) {
        System.out.println( "TRACE - contextInitialized " + Instant.now() );

        // Database.
        MyDatabaseService db = new MyDatabaseService();
        db.establishDatabase();
    }

    @Override
    public void contextDestroyed ( ServletContextEvent servletContextEvent ) {
        // This method intentionally left blank.
    }
}

数据库服务类

定义并预加载数据库。提供插入和查询,将数据移入和移出数据库。

package com.basilbourque.example;

import java.sql.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MyDatabaseService {
    // ---------|  Members  |------------------------------------
    static final private String driverName = "org.h2.Driver";
    static final private String catalogName = "ProdPop";
    static final private String jdbcPath = "jdbc:h2:mem:" + MyDatabaseService.catalogName + ";DB_CLOSE_DELAY=-1";  // "jdbc:h2:mem:autogrid";  // Set delay to keep in-memory database even after last connection closed.
    static final private String productTableName = "product_";
    static final private String orderTableName = "order_";

    public void establishDatabase () {
        // Verify JDBC driver.
        try {
            Class.forName( MyDatabaseService.driverName );
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        }

        // Connect, and create database.
        try ( Connection conn = DriverManager.getConnection( MyDatabaseService.jdbcPath ) ;
        ) {
            String sql = null;

            // Create product_ table.
            // Columns: pkey_  name_
            try ( Statement stmt = conn.createStatement() ; ) {
                sql = "CREATE TABLE " + productTableName + " ( \n" +
                      " pkey_ IDENTITY PRIMARY KEY , \n" +
                      " name_ VARCHAR ( 80 ) NOT NULL \n" +
                      ") ; \n";
                System.out.println( "TRACE - SQL:\n" + sql );
                stmt.execute( sql );
            }
            System.out.println( "TRACE - Created table product_." );

            // Create order_ table.
            // Columns: pkey_  fkey_product_  when_ordered_
            try ( Statement stmt = conn.createStatement() ; ) {
                sql = "CREATE TABLE " + orderTableName + " ( \n" +
                      "  pkey_ IDENTITY PRIMARY KEY  , \n" +
                      "  fkey_product_ LONG NOT NULL , \n" +
                      "  when_ordered_  TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP  \n" +
                      ") ; \n" +
                      "ALTER TABLE " + orderTableName + " ADD FOREIGN KEY ( fkey_product_ ) REFERENCES product_ ( pkey_ ) ; \n "
                ;
                System.out.println( "TRACE - SQL:\n" + sql );
                stmt.execute( sql );
            }

            // List tables
            DatabaseMetaData md = conn.getMetaData();
            try ( ResultSet rs = md.getTables( null , null , null , null ) ) {
                while ( rs.next() ) {
                    System.out.println( rs.getString( 3 ) );
                }
            }

            // List columns of `product_` table.
            try ( ResultSet rs = md.getColumns( null , null , productTableName.toUpperCase( Locale.US ) , null ) ) {
                System.out.println( "Columns of table: " + productTableName );
                while ( rs.next() ) {
                    System.out.println( rs.getString( 4 ) + " | " + rs.getString( 5 ) + " | " + rs.getString( 6 ) ); // COLUMN_NAME, DATA_TYPE , TYPE_NAME.
                }
            }

            // List columns of `order_` table.
            try ( ResultSet rs = md.getColumns( null , null , orderTableName.toUpperCase( Locale.US ) , null ) ) {
                System.out.println( "Columns of table: " + orderTableName );
                while ( rs.next() ) {
                    System.out.println( rs.getString( 4 ) + " | " + rs.getString( 5 ) + " | " + rs.getString( 6 ) ); // COLUMN_NAME, DATA_TYPE , TYPE_NAME.
                }
            }

            // Add rows
            sql = "INSERT INTO product_ ( name_ ) \n" +
                  "VALUES ( ? ) " +
                  "; ";

            List< String > planets = List.of( "Mercury" , "Venus" , "Earth" , "Mars" , "Ceres" , "Jupiter" , "Saturn" , "Uranus" , "Neptune" , "Pluto" );
            try ( PreparedStatement ps = conn.prepareStatement( sql ) ; ) {
                for ( String planet : planets ) {
                    ps.setString( 1 , planet );
                    ps.executeUpdate();
                }
            }

            System.out.println( "TRACE - Dumping tables in their initial state. " + Instant.now() );
            this.dumpTableToConsole( MyDatabaseService.productTableName );
            this.dumpTableToConsole( MyDatabaseService.orderTableName );
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
    }

    public void dumpTableToConsole ( String tableName ) {

        try ( Connection conn = DriverManager.getConnection( MyDatabaseService.jdbcPath ) ;
        ) {
            System.out.println( "TRACE - « " + tableName + " » table dump to console at " + Instant.now() );
            String sql = "SELECT * FROM " + tableName + " ;";
            try ( Statement stmt = conn.createStatement() ;
                  ResultSet rs = stmt.executeQuery( sql ) ; ) {
                ResultSetMetaData meta = rs.getMetaData();
                int colCount = meta.getColumnCount();
                int rowCount = 0;
                while ( rs.next() ) {
                    rowCount++;
                    System.out.print( "Row # " + rowCount + ": " );
                    for ( int col = 1 ; col <= colCount ; col++ ) {
                        System.out.print( meta.getColumnLabel( col ) + "=" );
                        Object o = rs.getObject( col );
                        if ( null != o ) {
                            System.out.print( o.toString() + " " );
                        }
                    }
                    System.out.println( "" ); // Newline.
                }
                System.out.println( "« fin de " + tableName + " »" );
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
    }

    public List< Product > fetchAllProducts () {
        System.out.println( "TRACE MyDatabaseService::fetchAllOrders at " + Instant.now() );

        List< Product > products = new ArrayList<>();

        // Query. Loop ResultSet, instantiating object, and collecting.
        try ( Connection conn = DriverManager.getConnection( MyDatabaseService.jdbcPath ) ;
        ) {
            System.out.println( "TRACE - fetchAllProducts at " + Instant.now() );
            String sql = "SELECT * FROM " + productTableName + " ;";
            try (
            Statement stmt = conn.createStatement() ;
            ResultSet rs = stmt.executeQuery( sql ) ;
            ) {
                int rowCount = 0;
                while ( rs.next() ) {
                    Long pkey = rs.getLong( "pkey_" );
                    String name = rs.getString( "name_" );
                    // Make object from column values.
                    Product p = new Product( pkey , name );
                    products.add( p ); // Collect each `Order` object retrieved from database.
                }
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

        return products;
    }

    public List< Order > fetchAllOrders () {
        System.out.println( "TRACE MyDatabaseService::fetchAllOrders at " + Instant.now() );

        List< Order > orders = new ArrayList<>();

        // Query. Loop ResultSet, instantiating object, and collecting.
        try ( Connection conn = DriverManager.getConnection( MyDatabaseService.jdbcPath ) ;
        ) {
            String sql = "SELECT * FROM " + orderTableName + " \n ORDER BY pkey_ DESC \n ;";
            try (
            Statement stmt = conn.createStatement() ;
            ResultSet rs = stmt.executeQuery( sql ) ;
            ) {
                int rowCount = 0;
                while ( rs.next() ) {
                    Long pkey = rs.getLong( "pkey_" );
                    Long fkey_product = rs.getLong( "fkey_product_" );
                    Instant when_ordered = rs.getObject( "when_ordered_" , Instant.class );
                    // Make object from column values.
                    Order o = new Order( pkey , fkey_product , when_ordered );
                    orders.add( o ); // Collect each `Order` object retrieved from database.
                }
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }

        return orders;
    }

    public void insertOrder ( Long fkeyProduct ) {
        System.out.println( "TRACE - MyDatabaseService::insertOrder at " + Instant.now() );
        try ( Connection conn = DriverManager.getConnection( MyDatabaseService.jdbcPath ) ;
        ) {
            String sql = "INSERT INTO " + orderTableName + "( fkey_product_ )\n" + " VALUES ( ? ) ;\n";
            PreparedStatement ps = conn.prepareStatement( sql );
            ps.setLong( 1 , fkeyProduct );
            ps.executeUpdate();
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
    }
}

内容布局

显示我们的小部件:NativeSelect https://vaadin.com/api/framework/8.5.2/com/vaadin/ui/NativeSelect.html产品下拉列表、订购按钮和一对Grid https://vaadin.com/api/framework/8.5.2/com/vaadin/ui/Grid.html数据网格。充当视图和控制器,用于与我们的用户交互。

package com.basilbourque.example;

import com.vaadin.data.HasValue;
import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.ui.*;

import java.time.Instant;
import java.util.List;

public class ProductPickerLayout extends VerticalLayout {
    Label prodPopLabel;
    NativeSelect< Product > productSelect;
    Button orderButton;

    Grid< Product > productsGrid;
    Grid< Order > ordersGrid;

    // Constructor
    public ProductPickerLayout () {
        this.widgetsMake();
        this.widgetsArrange();
    }

    private void widgetsMake () {

        // Create the selection component
        this.prodPopLabel = new Label( "Products: " );
        this.productSelect = new NativeSelect<>();
        // Add some items
        List< Product > products = new MyDatabaseService().fetchAllProducts();
        this.productSelect.setItems( products );
        this.productSelect.setItemCaptionGenerator( Product :: getName );
        // Show 5 items and a scrollbar if there are more
//        select.setRows( 3 );

        productSelect.addValueChangeListener( new HasValue.ValueChangeListener< Product >() {
            @Override
            public void valueChange ( ValueChangeEvent< Product > valueChangeEvent ) {
                Product p = valueChangeEvent.getValue();
                orderButton.setEnabled( null != p );
                Notification.show( "Selected: " + p.name );
            }
        } );

        this.orderButton = new Button( "Order" );
        this.orderButton.setEnabled( this.productSelect.getValue() != null );
        this.orderButton.addClickListener( ( Button.ClickEvent e ) -> {
            this.placeOrder();
            this.ordersGrid.setItems( new MyDatabaseService().fetchAllOrders() );
        } );

        MyDatabaseService db = new MyDatabaseService();

        this.productsGrid = new Grid<>( Product.class );
        this.productsGrid.setItems( products );
        this.productsGrid.setCaption( "Products" );

        this.ordersGrid = new Grid<>( Order.class );
        List< Order > orders = db.fetchAllOrders();
        this.ordersGrid.setItems( orders );
        this.ordersGrid.setCaption( "Orders" );
    }

    private void widgetsArrange () {
        HorizontalLayout orderBar = new HorizontalLayout();
        orderBar.setSpacing( true );
        orderBar.addComponents( this.prodPopLabel , this.productSelect , this.orderButton );
        orderBar.setComponentAlignment( this.prodPopLabel , Alignment.MIDDLE_CENTER );
        orderBar.setComponentAlignment( this.productSelect , Alignment.MIDDLE_CENTER );
        orderBar.setComponentAlignment( this.orderButton , Alignment.MIDDLE_CENTER );

        HorizontalLayout gridsBar = new HorizontalLayout();
        gridsBar.setSpacing( true );
        gridsBar.addComponents( this.productsGrid , this.ordersGrid );

        this.addComponents( orderBar , gridsBar );
        this.setExpandRatio( gridsBar , 1.0F );
    }

    private void placeOrder () {
        // Get pkey of the currently selected product.

        Product p = this.productSelect.getValue();
        if ( null == p ) {
            throw new IllegalStateException( "The `productSelect` NativeSelect does not have a current value." );
        }
        Long fkeyProduct = p.pkey;

        // Insert row into table.
        new MyDatabaseService().insertOrder( fkeyProduct );

        this.updateOrdersGrid();
    }

    private void updateOrdersGrid () {
    }
}

模型类——Product & Order

Vaadin 自动检测并使用 getter/setter 访问器方法来显示Grid数据网格小部件。当然,您也可以进行手动配置。

Our Product class.

package com.basilbourque.example;

public class Product {
    Long pkey;
    String name;

    public Product ( Long pkey , String name ) {
        this.pkey = pkey;
        this.name = name;
    }

    @Override
    public String toString () {
        return "Product{ " +
               "pkey=" + pkey +
               "| name='" + name +
               " }";
    }


    // -----------|  Accessors  |-----------------

    public Long getPkey () {
        return pkey;
    }

    public void setPkey ( Long pkey ) {
        this.pkey = pkey;
    }

    public String getName () {
        return name;
    }

    public void setName ( String name ) {
        this.name = name;
    }
}

Our Order class.

package com.basilbourque.example;

import java.time.Instant;

public class Order {
    Long pkey;  // Identifier of this order.
    Long fkeyProduct; // Identifier of the product being ordered.
    Instant whenOrdered; // The moment the order was placed.

    public Order ( Long pkey , Long fkeyProduct , Instant whenOrdered ) {
        this.pkey = pkey;
        this.fkeyProduct = fkeyProduct;
        this.whenOrdered = whenOrdered;
    }

    @Override
    public String toString () {
        return "Order{ " +
               "pkey=" + pkey +
               "| fkeyProduct=" + fkeyProduct +
               "| whenOrdered=" + whenOrdered +
               " }";
    }

    // -----------|  Accessors  |-----------------

    public Long getPkey () {
        return pkey;
    }

    public void setPkey ( Long pkey ) {
        this.pkey = pkey;
    }

    public Long getFkeyProduct () {
        return fkeyProduct;
    }

    public void setFkeyProduct ( Long fkeyProduct ) {
        this.fkeyProduct = fkeyProduct;
    }

    public Instant getWhenOrdered () {
        return whenOrdered;
    }

    public void setWhenOrdered ( Instant whenOrdered ) {
        this.whenOrdered = whenOrdered;
    }
}

Maven POM

The pom.xml文件、控制Maven https://en.wikipedia.org/wiki/Apache_Maven用于加载依赖项(库)和构建/运行 Web 应用程序。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.basilbourque.example</groupId>
    <artifactId>prodpop</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>prodpop</name>

    <prerequisites>
        <maven>3</maven>
    </prerequisites>

    <properties>
        <vaadin.version>8.5.2</vaadin.version>
        <vaadin.plugin.version>8.5.2</vaadin.plugin.version>
        <jetty.plugin.version>9.4.12.v20180830</jetty.plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <!-- If there are no local customizations, this can also be "fetch" or "cdn" -->
        <vaadin.widgetset.mode>local</vaadin.widgetset.mode>
    </properties>

    <repositories>
        <repository>
            <id>vaadin-addons</id>
            <url>http://maven.vaadin.com/vaadin-addons</url>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-push</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiled</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
        </dependency>

        <!--Basil-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <!-- Exclude an unnecessary file generated by the GWT compiler. -->
                    <packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>update-theme</goal>
                            <goal>update-widgetset</goal>
                            <goal>compile</goal>
                            <!-- Comment out compile-theme goal to use on-the-fly theme compilation -->
                            <goal>compile-theme</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <!-- Clean up also any pre-compiled themes -->
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/webapp/VAADIN/themes</directory>
                            <includes>
                                <include>**/styles.css</include>
                                <include>**/styles.scss.cache</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <!-- The Jetty plugin allows us to easily test the development build by
                running jetty:run on the command line. -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.plugin.version}</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Vaadin pre-release repositories -->
            <id>vaadin-prerelease</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>

            <repositories>
                <repository>
                    <id>vaadin-prereleases</id>
                    <url>http://maven.vaadin.com/vaadin-prereleases</url>
                </repository>
                <repository>
                    <id>vaadin-snapshots</id>
                    <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>vaadin-prereleases</id>
                    <url>http://maven.vaadin.com/vaadin-prereleases</url>
                </pluginRepository>
                <pluginRepository>
                    <id>vaadin-snapshots</id>
                    <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

</project>

顺便说一句,在 macOS 世界中,“下拉列表”被称为“弹出菜单”或“弹出窗口”。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java上将数据库中的数据以表单的形式输出给用户 的相关文章

  • 以编程方式将 PEM 证书导入 Java KeyStore

    我有一个由两个文件 crt 和 key 组成的客户端证书 我希望将其导入到 java KeyStore 中 然后在 SSLContext 中使用 以通过 Apache 的 HTTPClient 发送 HTTP 请求 但是 我似乎找不到一种以
  • Spring webflow 应用程序:HTTP 302 暂时移动

    我的 java 应用程序中的每个请求都会生成另外 2 个带有 HTTP 302 错误的请求 例如 如果请求查看名为板 html 这个请求是从首页 html 我收到按以下顺序生成的 3 个请求 POST home html 302 Moved
  • 有效地查找正则表达式的所有重叠匹配项

    这是后续与 java 正则表达式匹配的所有重叠子字符串 https stackoverflow com q 11303309 244526 有没有办法让这段代码更快 public static void allMatches String
  • Maven + Cobertura:无法找到[您的班级]。你指定了源目录吗?

    我有 MyMath 类 有两个简单的方法 multi 和 add 和测试类只会测试多种方法 public class MainTest Test public void testMultiply MyMath tester new MyMa
  • java“void”和“非void”构造函数

    我用 java 编写了这个简单的类 只是为了测试它的一些功能 public class class1 public static Integer value 0 public class1 da public int da class1 v
  • 从 eclipse 运行时 java.io.FileNotFoundException: (没有这样的文件或目录)

    我正在写入文件并想要控制台输出 TODO Create a game engine and call the runGame method public static void main String args throws Excepti
  • firestore快照监听器生命周期和定价之间有什么关系?

    在我的活动中 我有一个字符串列表 这些字符串表示我想要附加快照侦听器的 Firestore 文档 我使用 Acivity ModelView 存储库结构 在活动的 onCreate 中 我向 ViewModelProvider 询问适当的
  • 如何消除警告:使用“$”而不是“.”对于 Eclipse 中的内部类

    我是 Android 开发新手 当我将 eclipse 和 Android SDK 更新到最新版本后 我收到警告 Use instead of for inner classes or use only lowercase letters
  • 会话 bean 中的 EntityManager 异常处理

    我有一个托管无状态会话 bean 其中注入了 EntityManager em 我想做的是拥有一个具有唯一列的数据库表 然后我运行一些尝试插入实体的算法 但是 如果实体存在 它将更新它或跳过它 我想要这样的东西 try em persist
  • JSP 标签+ scriptlet。如何启用脚本?

    我有一个使用标签模板的页面 我的 web xml 非常基本 我只是想在页面中运行一些代码 不 我对标签或其他替代品不感兴趣 我想使用不好的做法 scriptlet 哈哈 到目前为止 我收到了 HTTP ERROR 500 错误 Script
  • java 属性文件作为枚举

    是否可以将属性文件转换为枚举 我有一个包含很多设置的属性文件 例如 equipment height equipment widht equipment depth and many more like this and not all a
  • Java中的DRY原则[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我一直在读关于DRY https en wikipedia org wiki Don 27t repeat yourself原则 虽然看起来
  • 使用外部硬盘写入和存储 mysql 数据库

    我已经设置了 mysql 数据库在我的 Mac 上使用 java 和 eclipse 运行 它运行得很好 但现在我将生成大约 43 亿行数据 这将占用大约 64GB 的数据 我存储了大量的密钥和加密值 我有一个 1TB 外部我想用作存储位置
  • 通用 JSF 实体转换器[重复]

    这个问题在这里已经有答案了 我正在编写我的第一个 Java EE 6 Web 应用程序作为学习练习 我没有使用框架 只是使用 JPA 2 0 EJB 3 1 和 JSF 2 0 我有一个自定义转换器 用于将存储在 SelectOne 组件中
  • @TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试

    似乎我在 Spring 4 1 17 中使用 Spring Boot 1 2 6 RELEASE 所做的任何事情都不起作用 我只想访问应用程序属性并在必要时通过测试覆盖它们 无需使用 hack 手动注入 PropertySource 这不行
  • 将字符串中的字符向左移动

    我是 Stack Overflow 的新手 有一道编程课的实验室问题一直困扰着我 该问题要求我们将字符串 s 的元素向左移动 k 次 例如 如果输入是 Hello World 和3 它将输出 lo WorldHel 对于非常大的 k 值 它
  • Android 中的字符串加密

    我正在使用代码进行加密和加密 它没有给出字符串结果 字节数组未转换为字符串 我几乎尝试了所有方法将字节数组转换为字符 但没有给出结果 public class EncryptionTest extends Activity EditText
  • 防止 Firebase 中的待处理写入事务不起作用

    我的目标是在单击按钮时将名称插入 Cloud Firestore 中 但如果用户未连接到互联网 我不希望保存处于挂起状态 我不喜欢 Firebase 保存待处理写入的行为 即使互联网连接已恢复 我研究发现Firebase 开发人员建议使用事
  • oracle日期序列?

    我有一个 oracle 数据库 我需要一个包含 2 年所有日期的表 例如来自01 01 2011 to 01 01 2013 首先我想到了一个序列 但显然唯一支持的类型是数字 所以现在我正在寻找一种有效的方法来做到这一点 欢呼骗局 如果您想
  • Java泛型类型

    当我有一个界面时 public interface Foo

随机推荐