0

======NewContact.jsp==============0

Here I'm getting the Model object and rendering happly.

======Users.java(Model Class)======1

package com.one.model;
import java.util.ArrayList;
public class Users
{

  private ArrayList<String> al;
  private String x;
  private int y;

  // getters and setters

   }

===============UsersDao.java(Dao interface)========2

package com.one.dao;
import com.one.model.Users;
public interface UsersDao {
   public Users getModified(Users glist);
   }
}

==============UsersDaoImpl.java(Dao Implementation)=======3

package com.one.daoimpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import com.one.dao.UsersDao;
import com.one.model.Users;

public class UsersDaoImpl implements UsersDao {

private JdbcTemplate jdbcTemplate;
public UsersDaoImpl(DataSource dataSource) 
{
    jdbcTemplate = new JdbcTemplate(dataSource);
}


@Override
public Users getModified(Users glist)
{
    String sql1 = "SELECT user_group FROM users";
   //Here I was strucked.
    return glist;
}

}

Here I was strucked. I have JdbcTemplate class obj. This is Spring MVC application. I'm able to executing other queries here,no problem. And displaying the all the values in the view pages no problem. But I need get drop down menu in NewContact.jsp. For that I'm creating Model object in Controller class and Sending that model object to the NewContact.jsp i.e view page. but how can I inject only user_group values from the databse table,assume "users"(is table) and "name,sal,user_group"(are the columns OF "users" table).

Finally, when I am returning an object(i.e object of the Model'Users.java') from the UsersDaoImpl class,only model object "ArrayList al" must be initialized. But x and y variables should not be initialized. I Don't want to use AJAX here.

SWiggels
  • 2,159
  • 1
  • 21
  • 35
Kodali444
  • 1,283
  • 1
  • 17
  • 21
  • I've edited your question to format code as code, because you're new here. Next time, format it correctly, and don't post it until it looks correct. – JB Nizet Sep 02 '15 at 12:53
  • Oh! sorry. I'm very newer to this environment. Thank you – Kodali444 Sep 02 '15 at 13:06

2 Answers2

0

What you can do here is first to create a DAO implementation that returns a list of objects that you want to display in the drop down menu.Then use Spring annotation @ResponseBody in one of your controller method, and return a json array that contains the values that you want to included in your dropdown.

    @RequestMapping(value = "/url", method = RequestMethod.GET)
    public @ResponseBody List<Object> getObject() {
        //from here call the DAO implementation for returning the object, even tho you suppose to include a service layer.
    }

Now, you gonna need use some jQuery in your JSP:

A simple drop down can be like this:

<script type="text/javascript">
        $(document).ready(
            function() {
                $.getJSON('<spring:url value="your JSON file URL path here"/>', {
                    ajax : 'true'
                }, function(data){
                    var html = '<option value="">--Please select one--</option>';
                    var len = data.length;
                    for (var i = 0; i < len; i++) {
                        html += '<option value="' + data[i].desc + '">'
                                + data[i].desc + '</option>';
                    }
                    html += '</option>';

                    $('#dropDownSelect').html(html);
                });

            });

    </script>

And JSP:

<select id="dropDownSelect"/>
OPK
  • 4,120
  • 6
  • 36
  • 66
  • Is it possible without using JQuery,Ajax – Kodali444 Sep 02 '15 at 13:11
  • Sir, Actually I don't know anything about JQuery,Ajax..etc. – Kodali444 Sep 02 '15 at 13:12
  • You can have a look at this if u don't want to use jquery, should get you started pretty good: http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/ – OPK Sep 02 '15 at 13:14
  • Insted of sending only new object to NewContact.jsp ,I need to send Model Object , in which only ArrayList attribute is initializes with values – Kodali444 Sep 02 '15 at 13:16
  • No, you can create a list that only contains `UserGroups`, for example, `List list;` And you just need to create a DAO implementation, such as this: `public List getUserGroups(){ ...here you use JDBC to return the data...}` – OPK Sep 02 '15 at 13:26
  • public List list() { String sql = "SELECT * FROM groups"; List listContact = jdbcTemplate.query(sql, new RowMapper() { @Override public Groups mapRow(ResultSet rs, int rowNum) throws SQLException { Groups glist = new Groups(); glist.setId(rs.getInt("id")); glist.setName(rs.getString("name")); glist.setIs_active(rs.getInt("is_active")); glist.setTenant_id(rs.getLong("tenant_id")); return glist; } }); return listContact; } – Kodali444 Sep 02 '15 at 13:45
  • public Users getModified(Users glist) { String sqlQuery = "SELECT user_group FROM users"; //++++ return glist; } //++++ here which method of jdbcTemplate class I have to use to pass "sqlQuery" and retrive the 'user_group' column of 'users' table and assign those 'user_group' values to attribute ArrayList of Users Model object. Here I no need to assign any values to 'x and y' of Users Model object. If once this over, then I will get the Users Model Object in New Contacts.jsp with 'x and y' I get the values from user. and "ArrayList" contains the values from DB. – Kodali444 Sep 02 '15 at 14:02
  • r u using jdbc template? if you are, see this: http://stackoverflow.com/questions/8966821/return-a-list-i-already-have-a-rowmapper-implementation – OPK Sep 02 '15 at 14:07
  • I think that code is to return the list of objects. my requirement is to return only one object . In that one object only one attribute value initialized. – Kodali444 Sep 02 '15 at 14:37
0

I think your best choice is create a form-class to initialize the values after get it. In controller, before adding the form into the model, set the list and give the others at null.

Kratul
  • 158
  • 2
  • 13
  • Can you please provide related url's. Then I can easily get relevant information. I mean i din't use form-class so far. – Kodali444 Sep 02 '15 at 14:35