jeudi 15 juin 2017

How to update user`s data in Spring MVC

I am working on the application in which users can set the places their visited and view them in lists and on the maps. So I wanted to give an opportunity to the user to update lists of cities. I have methods in data access layer with the database connection. And I have my JSP page with checkboxes, which can be selected by the user. The only problem is that I do not know how to connect these parts. I have controller class (as I use MVC Spring framework) with the method for this form, but I am still confused whether I need to put the code here or to use some JS script. So I would be very grateful if you could help me with that!

DAO layer methods:

Service
@Transactional
public class UserInfoDAOImpl extends JdbcDaoSupport implements UserInfoDAO {
    @Autowired
    public UserInfoDAOImpl(DataSource dataSource) {
        this.setDataSource(dataSource);
    }


    @Override
    public List<String> getVisitedCities(String userName){
        String sql = "Select DISTINCT p.City from Users_Places up "
                + " join Places p on up.PID = p.P_ID "
                + " join Users u on up.UID = u.U_ID "
                + " where u.Username = ? "
                + " order by p.City;";
        Object [] params = new Object[] { userName };
        List<String> cities = this.getJdbcTemplate().queryForList(sql, params, String.class);
        return cities;
    }

    @Override
    public List<String> selectCities(String userName) {
        String sql = "select DISTINCT City from Places "; 
        //Object[] params = new Object[] { userName };

        List<String> allCities = this.getJdbcTemplate().queryForList(sql, String.class);
        return allCities;
    }

        @Override
    public void editCities(String userName, String city) {
        String sql = "insert into Users_Places (Uid, Pid)"
                + " values ((select U_ID from Users where Username = ?),"
                + "(select P_ID from Places where City = ? ));";
        Object[] params = new Object[] {userName, city};
        this.getJdbcTemplate().update(sql, params);
    }
}

Main controller:

@Controller public class MainController {

    @Autowired
    private UserInfoDAO userInfoDAO;

    @RequestMapping(value = "/editPlaces", method = RequestMethod.GET)
       public String editPlaces(Model model, Principal principal) {
           String userName = principal.getName();
           List<String> citiesList = userInfoDAO.getVisitedCities(userName);
           List<String> allCities = userInfoDAO.selectCities(userName);

           model.addAttribute("allCities", allCities);
           model.addAttribute("citiesList", citiesList);
           model.addAttribute("notVisCo", notVisitedCo);
           return "editPlaces";
       }
}

JSP page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.List, java.util.Arrays" %>
<%@ taglib  uri="http://ift.tt/QfKAz6" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <jsp:include page="header.jsp"/>    
    <jsp:include page="_menu.jsp" />

    <h4>Select cities you have visited</h4>
    <form name="cities" method='POST'>
                  <c:forEach items="${allCities}" var="item">
                    <input type=checkbox name='city' value="${item}" <%=("${citiesList}".contains("${item}") ? ("checked='checked'") : "")%> />${item}<br>
                  </c:forEach><br>
        <input type="submit" value="Submit"/>
    </form>

    <jsp:include page="footer.jsp"/> 
</body>




Aucun commentaire:

Enregistrer un commentaire