Map<String, List<String>> mapOfList = new TreeMap<>();
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
@Controller
public class SearchCommentController {
...................
@RequestMapping(method = RequestMethod.POST)
public String displayCommentAlert(@Validated FormParams params, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors() == false) {
List<CommentWithPeopleDetail> foundComments = commentDao.find(params);
model.addAttribute("foundComments", foundComments);
}
return "commentAlert";
}
...................
}
public class SearchCommentParamValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return FormParams.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
FormParams params = (FormParams) target;
if (StringUtils.isBlank(params.getRuleId()) && StringUtils.isBlank(params.getRef())) {
errors.reject(null, "Необходимо указать или 'ID правила', и/или 'Объект алерта'!");
} else if (params.getDateFrom() == null || params.getDateTo() == null) {
errors.reject(null, "Необходимо указать период поиска!");
}
}
}
<mvc:annotation-driven/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set merge="true">
<bean class="my.app.OptionConverter"/>
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>
import java.sql.*;
public class H2Main {
static {
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Error load H2 JDBC driver: " + e.getMessage());
}
}
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:h2:./h2example", "sa", "")) {
createSimpleDBSchema(con);
System.out.printf("Message from H2: %s\n", getMessage(con));
} catch (Exception e) {
System.err.printf("%s: %s\n", e.getClass().getSimpleName(), e.getMessage());
}
}
static void createSimpleDBSchema(Connection con) {
try (Statement stmt = con.createStatement()) {
stmt.executeUpdate("CREATE TABLE HelloH2 (message varchar(255) NULL);");
stmt.executeUpdate("INSERT INTO HelloH2 (message) VALUES ('Hello World!')");
} catch (SQLException e) {
System.err.printf("%s: %s\n", e.getClass().getSimpleName(), e.getMessage());
}
}
static String getMessage(Connection con) throws SQLException {
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT message FROM HelloH2")) {
if (rs.next())
return rs.getString("message");
else
return null;
}
}
}
import java.util.Arrays;
public class IntMatrix {
private final int[] lineMatrix;
private final int n;
private final int m;
public IntMatrix(int[] lineMatrix, int n, int m) {
if (n < 0 || m < 0 || lineMatrix.length != n * m)
throw new IllegalArgumentException();
this.lineMatrix = lineMatrix.clone();
this.n = n;
this.m = m;
}
public int get(int i, int j) {
if (i < n && j < m)
return lineMatrix[i * m + j];
else
throw new ArrayIndexOutOfBoundsException();
}
public void set(int value, int i, int j) {
if (i < n && j < m)
lineMatrix[i * m + j] = value;
else
throw new ArrayIndexOutOfBoundsException();
}
public void sort() {
// готовая реализация быстрой сортировки
Arrays.sort(lineMatrix);
}
public int getN() {
return n;
}
public int getM() {
return m;
}
}
public static void main(String[] args) {
int n = 10, m = 15;
IntMatrix intMatrix = new IntMatrix(new int[n * m], n, m);
fillMatrixByRandomValues(intMatrix);
printMatrix(intMatrix);
intMatrix.sort();
System.out.println("---------------");
printMatrix(intMatrix);
}
/** заполнение матрицы случайными числами */
static void fillMatrixByRandomValues(IntMatrix matrix) {
Random random = new Random();
for (int i = 0; i < matrix.getN(); i++) {
for (int j = 0; j < matrix.getM(); j++) {
matrix.set(random.nextInt(1000), i, j);
}
}
}
/** печать матрицы в консоль */
static void printMatrix(IntMatrix matrix) {
for (int i = 0; i < matrix.getN(); i++) {
for (int j = 0; j < matrix.getM(); j++) {
System.out.printf("%s\t", matrix.get(i , j));
}
System.out.println();
}
}
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(TestServlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/test.jsp").forward(req, resp);
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
long id = Long.parseLong(req.getParameter("id"));
testRemove(id);
resp.setStatus(200);
} catch (Exception e) {
LOG.error("Error remove: {}", e.getMessage());
resp.setStatus(500);
}
}
private void testRemove(long id) {
if (id < 1) {
throw new RuntimeException("id can't be < 1");
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>
<input type="button" value="remove data (OK)" onclick="remove(2)"/>
</p>
<p>
<input type="button" value="remove data (ERROR)" onclick="remove(-1)"/>
</p>
<script>
function remove(id) {
var request = new XMLHttpRequest();
request.open('DELETE', '/test?id=' + id, false);
request.send();
if (request.status == 200) {
alert('Данные упешно удалены')
} else {
alert('Ошибка удаления данных!');
}
}
</script>
</body>
</html>
ContractServiceImpl contractService = new ContractServiceImpl();
List<Contract> contracts = contractService.getAllContractsForUser(user.getUserId());
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ContractServiceImpl contractService = new ContractServiceImpl();
List<Contract> contracts = contractService.getAllContractsForUser(user.getUserId());
request.setAttribute("contracts",contracts);
RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/you_jsp_page.jsp");
requestDispatcher.forward(request, response);
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<hmtl>
...
<body>
<table>
<c:forEach items="${contracts}" var="contr">
<tr>
<td>${contr.name}</td>
<td>${contr.number}</td>
....
</tr>
</c:forEach>
</table>
</body>
</hmtl>
<form method="post" action="ваш сервлет">
<input type="submit" value="Удалить выбранное"/>
<table>
<c:forEach items="${contracts}" var="contr">
<tr>
<td><input type="checkbox" name="id" value="${contr.id}"/></td>
<td>${contr.name}</td>
<td>${contr.number}</td>
....
</tr>
</c:forEach>
</table>
</form>
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] ids = req.getParameterValues("id");
if (ids != null && ids.length > 0) {
ContractServiceImpl contractService = new ContractServiceImpl();
contractService.removeAll(ids);
}
doGet(req, resp);
}
public final class User {
private final String login;
private final Date dateCreate;
private final String description;
private final Set<String> roles;
public User(String login, Date dateCreate, String description, Set<String> roles) {
this.login = login;
this.dateCreate = (Date) dateCreate.clone();
this.description = description;
this.roles = Collections.unmodifiableSet(roles);
}
public String getLogin() {
return login;
}
public Date getDateCreate() {
return (Date) dateCreate.clone();
}
public String getDescription() {
return description;
}
public Set<String> getRoles() {
return roles;
}
}
public class UserFactory<T> {
private final AtomicReference<T> atomicReference;
public UserFactory(T object) {
this.atomicReference = new AtomicReference<>(object);
}
public T getObject() {
return atomicReference.get();
}
public void setObject(T object) {
atomicReference.set(object);
}
}
public static void main(String[] args) {
User user = new User("user1", new Date(), "test user", new HashSet<>(Arrays.asList("READ", "ADD")));
UserFactory<User> userFactory = new UserFactory<>(user);
exampleUsingUser(userFactory);
}
public static void exampleUsingUser(UserFactory<User> userFactory) {
User user = userFactory.getObject();
System.out.println(user.getLogin());
System.out.println(user.getDescription());
System.out.println(user.getRoles());
}
public interface Settings {
String getColor();
void setColor(String color);
String getDescription();
void setDescription(String description);
int getSize();
void setSize(int size);
}
public class SettingsImpl implements Settings {
private volatile String color;
private volatile String description;
private volatile int size;
public static Settings newSettings() {
return ProxySettingsKeeper.createProxy(new SettingsImpl());
}
private SettingsImpl() {
}
@Override
public String getColor() {
return color;
}
@Override
public void setColor(String color) {
this.color = color;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
@Override
public int getSize() {
return size;
}
@Override
public void setSize(int size) {
this.size = size;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ProxySettingsKeeper implements InvocationHandler {
private final Settings settings;
private final ReentrantReadWriteLock.ReadLock readLock;
private final ReentrantReadWriteLock.WriteLock writeLock;
public ProxySettingsKeeper(Settings settings) {
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
this.readLock = readWriteLock.readLock();
this.writeLock = readWriteLock.writeLock();
this.settings = settings;
}
public static Settings createProxy(Settings settings) {
return (Settings) Proxy.newProxyInstance(settings.getClass().getClassLoader()
, new Class[]{Settings.class}
, new ProxySettingsKeeper(settings));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
if (name.startsWith("get")) {
readLock.lock();
try {
return method.invoke(settings, args);
} finally {
readLock.unlock();
}
} else if (name.startsWith("set")) {
writeLock.lock();
try {
return method.invoke(settings, args);
} finally {
writeLock.unlock();
}
}
return method.invoke(settings, args);
}
}
public static void main(String[] args) {
Settings settings = SettingsImpl.newSettings();
settings.setDescription("test");
System.out.println(settings.getDescription());
}