0

I'm a newbie of web application.

I'm creating a web application following the Model-View-Controller pattern. I have MySQL database (Model), the jsp pages (Views) and a DAO (Controller).

How can I fetch data from database and dinamically create a table with that? Do I have to use JSP scriptlet and inside JavaScript? I read that it is not recommended.

Barb2
  • 13
  • 2
  • We need more info, are you trying to use any kind of web framework in your JSP? What's the idea? Do you want it to be rendered by the server, or do you want to expose as an API (using JSON for instance) and get your client side to do the work? – Luís Brito Nov 18 '18 at 17:34
  • @LuísBrito just want to expose it as an API. – Barb2 Nov 18 '18 at 17:39
  • Using the java server as a RESTful JSON API, you won't need JSP. The client-side can do all the work. You can also take a look into some web app frameworks, such as Vue.js, ReactJS or Angular. – Luís Brito Nov 18 '18 at 17:49

2 Answers2

0

One way to do it is have a class in your Models that represents a row of the database and have a method to read a row. Then you can have a Get method that returns a list of objects... something like...

public class MyItem
{
    public string Id { get; set; }
    public string MyProperty { get; set; }

    public List<MyItem> Get()
    {
        List<MyItem> items = new List<MyItem>();

        try
        {
            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                using (SqlCommand select = new SqlCommand())
                {
                    select.CommandText = @"select bla bla bla from bla whete MyProperty=@MyProperty";

                    select.Parameters.AddWithValue("@MyProperty", this.MyProperty);

                    select.Connection = connection;
                    connection.Open();

                    using (SqlDataReader reader = select.ExecuteReader())
                    {
                        DataTable dataTable = new DataTable("ResultSet");
                        dataTable.Load(reader);

                        if ((dataTable != null) && (dataTable.Rows != null) && (dataTable.Rows.Count > 0))
                        {
                            foreach (DataRow row in dataTable.Rows)
                            {
                                MyItem item = new MyItem();
                                item.FromRow(row);
                                items.Add();
                            }
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            log here
            throw;
        }
        return items;
    }
    public void FromRow(DataRow row)
    {
        Id = row["Id"].ToString();
        ...
    }
}

In your controller you can then do something like...

[Authorize(Roles = "...")]
public IActionResult GetMyItems(string id)
{
    MyItem item = new MyItem() { Id = id };
    var items = item.Get();

    return items;
}

Then you should be able to access using the Model property in your view.

JackSojourn
  • 324
  • 2
  • 15
0

Assuming you want to expose it as a JSON API with a independent client-side application, as talked in the comments. You'll need to make an AJAX HTTP request to your server. You could use a lot of different libraries for this, such as jQuery.


Getting the data

Start by requesting the data from your web server using jQuery.get:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
}); 

For a detailed reference, check the tutorial in jQuery documentation.

Rendering the data in the HTML

There are several ways to do it using jQuery, I recommend some of the following answers as a reference:

Luís Brito
  • 1,652
  • 1
  • 17
  • 34