edit user

This commit is contained in:
Anton Romanov 2020-02-22 09:03:18 +04:00
parent 2110c21be7
commit 34c5e79cbb
7 changed files with 175 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext; import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
public class FacesUtil { public class FacesUtil {
public static void redirectToPage(FacesContext context, String page, String params) { public static void redirectToPage(FacesContext context, String page, String params) {
@ -26,6 +27,11 @@ public class FacesUtil {
redirectToPage(FacesContext.getCurrentInstance(), page, params); redirectToPage(FacesContext.getCurrentInstance(), page, params);
} }
public static Map<String, String> getRequestParams() {
return FacesContext.getCurrentInstance().
getExternalContext().getRequestParameterMap();
}
public static void showInfoMessage(String summary, String detail) { public static void showInfoMessage(String summary, String detail) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
FacesContext fc = FacesContext.getCurrentInstance(); FacesContext fc = FacesContext.getCurrentInstance();

View File

@ -8,14 +8,12 @@ import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped; import javax.faces.view.ViewScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
@Named @Named
@ViewScoped @ViewScoped
@ -30,9 +28,7 @@ public class PaperView implements Serializable {
@PostConstruct @PostConstruct
public void init() { public void init() {
Map<String, String> params = FacesContext.getCurrentInstance(). paper = paperService.findPaperById(Integer.valueOf(FacesUtil.getRequestParams().get("id")));
getExternalContext().getRequestParameterMap();
paper = paperService.findPaperById(Integer.valueOf(params.get("id")));
FacesUtil.showInfoMessage("Статья открыта", ""); FacesUtil.showInfoMessage("Статья открыта", "");
} }

View File

@ -0,0 +1,21 @@
package ru.ulstu.user.controller;
import ru.ulstu.user.model.User;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(value = "userDegreeConverter")
public class UserDegreeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return User.UserDegree.valueOf(value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((User.UserDegree) value).name();
}
}

View File

@ -0,0 +1,39 @@
package ru.ulstu.user.controller;
import ru.ulstu.core.util.FacesUtil;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
@Named
@ViewScoped
public class UserView implements Serializable {
@Inject
private UserService userService;
private User user;
@PostConstruct
public void init() {
user = userService.findById(Integer.valueOf(FacesUtil.getRequestParams().get("id")));
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User.UserDegree> getDegrees() {
return Arrays.asList(User.UserDegree.values());
}
}

View File

@ -1,4 +1,4 @@
package ru.ulstu.user.component; package ru.ulstu.user.controller;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
@ -12,7 +12,7 @@ import java.util.List;
@Named @Named
@ViewScoped @ViewScoped
public class UserView implements Serializable { public class UsersView implements Serializable {
@Inject @Inject
private UserService userService; private UserService userService;

View File

@ -0,0 +1,105 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/basicTemplate.xhtml">
<ui:define name="header">
Редактирование статьи
</ui:define>
<ui:define name="content">
<style>
#mainForm\:paperStatus > tbody > tr {
border: none;
}
#mainForm\:paperStatus > tbody > tr > td {
border: none;
padding: 0px;
}
#mainForm\:paperPanel > tbody > tr {
border: none;
}
#mainForm\:paperPanel > tbody > tr > td {
border: none;
}
.ui-editor.ui-widget-content {
background: white !important;
}
</style>
<p:panel id="panel" header="Редактирование пользователя" style="margin-bottom:10px;">
<p:panelGrid cellpadding="5" id="userPanel">
<p:row>
<p:column>
<h:outputLabel value="Имя:"/>
</p:column>
<p:column>
<p:inputText id="firstName" required="true" value="#{userView.user.firstName}"/>
<p:message for="firstName"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Фамилия:"/>
</p:column>
<p:column>
<p:inputText id="lastName" required="true" value="#{userView.user.lastName}"/>
<p:message for="lastName"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Отчество:"/>
</p:column>
<p:column>
<p:inputText id="patr" value="#{userView.user.patronymic}"/>
<p:message for="patr"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Email:"/>
</p:column>
<p:column>
<p:outputLabel value="#{userView.user.email}"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Дата рождения:"/>
</p:column>
<p:column>
<p:calendar id="bDate" required="true" value="#{userView.user.birthDate}"/>
<p:message for="bDate"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel value="Ученая степень:"/>
</p:column>
<p:column>
<p:selectOneMenu id="degree" value="#{userView.user.degree}"
converter="#{userDegreeConverter}">
<f:attribute name="collectionType" value="java.util.HashSet"/>
<f:selectItem itemLabel="Нет" itemValue="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{userView.degrees}"
var="degree"
itemLabel="#{degree.degreeName}"
itemValue="#{degree}"/>
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>
<p:commandButton action="#{paperView.save}" value="Сохранить" ajax="true" process="@form"
update="messages @form"/>
</p:column>
</p:row>
</p:panelGrid>
</p:panel>
</ui:define>
</ui:composition>
</html>

View File

@ -10,7 +10,7 @@
text-align: right !important; text-align: right !important;
} }
</style> </style>
<p:dataTable value="#{userView.users}" var="user" paginator="true" rows="10" id="usersTable" <p:dataTable value="#{usersView.users}" var="user" paginator="true" rows="10" id="usersTable"
widgetVar="usersTable" emptyMessage="Не найдено пользователей"> widgetVar="usersTable" emptyMessage="Не найдено пользователей">
<f:facet name="header"> <f:facet name="header">
<p:outputPanel> <p:outputPanel>