Problema con botón aceptar
Publicado por María de Alejandría (2 intervenciones) el 02/05/2018 19:43:11
Saludos.
Me sucede lo siguiente:
Cuento con un formulario mediante el cual deseo dar de alta a un usuario registrándolo en una base de datos. Cuando selecciono "Aceptar" no pasa nada, el botón no funciona. Aquí el código usado:
Formulario
UserBackingBean
UserClientBean
package client;
import com.mycompany.pfinalpsegrupo2.Usuarios;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import json.UserReader;
import json.UserWriter;
@Named
@RequestScoped
public class UserClientBean {
@Inject
UserBackingBean bean;
Client client;
WebTarget target;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
target = client.target("http://localhost:8080/PFinalPSEGrupo2/webresources/com.mycompany.pfinalpsegrupo2.usuarios");
}
@PreDestroy
public void destroy() {
client.close();
}
public Usuarios[] getUsers() {
return target
.request()
.get(Usuarios[].class);
}
public Usuarios getUser() {
return target
.register(UserReader.class)
.path("{Email}")
.resolveTemplate("Email", bean.getEmail())
.request(MediaType.APPLICATION_JSON)
.get(Usuarios.class);
}
//No pone que el usuario pueda darse de baja,método a revisar
public void deleteUser() {
target.path("{Email}")
.resolveTemplate("Email", bean.getEmail())
.request()
.delete();
}
public void addUser() {
Usuarios u = new Usuarios();
u.setEmail(bean.getEmail());
u.setNombre(bean.getNombre());
u.setPassword(bean.password);
target.register(UserWriter.class)
.request()
.post(Entity.entity(u,MediaType.APPLICATION_JSON));
}
}
UserReader
package json;
import com.mycompany.pfinalpsegrupo2.Usuarios;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class UserReader implements MessageBodyReader<Usuarios> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Usuarios.class.isAssignableFrom(type);
}
@Override
public Usuarios readFrom(Class<Usuarios> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
Usuarios usuario = new Usuarios();
JsonParser parser = Json.createParser(entityStream);
while (parser.hasNext()) {
switch (parser.next()) {
case KEY_NAME:
String key = parser.getString();
parser.next();
switch (key) {
case "id":
usuario.setEmail(parser.getString());
break;
case "name":
usuario.setNombre(parser.getString());
break;
case "actors":
usuario.setPassword(parser.getString());
break;
default:
break;
}
break;
default:
break;
}
}
return usuario;
}
}
UserWriter
Gracias de antemano.
Me sucede lo siguiente:
Cuento con un formulario mediante el cual deseo dar de alta a un usuario registrándolo en una base de datos. Cuando selecciono "Aceptar" no pasa nada, el botón no funciona. Aquí el código usado:
Formulario
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<ui:composition template="./../WEB-INF/Template.xhtml">
<ui:define name="content">
<h:form id="form">
<p:panel id="panel" header="Form" style="margin-bottom:10px;">
<p:messages id="messages" />
<h:panelGrid columns="3" cellpadding="5">
<p:outputLabel for="Nombre" value="Nombre y apellidos:" />
<p:inputText id="Nombre" value="#{userBackingBean.nombre}" required="true" label="Nombre">
<f:validateLength minimum="2" />
</p:inputText>
<p:message for="Nombre" />
<p:outputLabel for="Email" value="Email" />
<p:inputText id="Email" value="#{userBackingBean.email}" required="true" label="Email"/>
<p:message for="Email" />
<p:outputLabel for="Password" value="Password" />
<p:inputText type="password" id="Password" value="#{userBackingBean.password}" required="true" label="Password"/>
<p:message for="Password" />
<p:outputLabel for="Password2" value="Confirmar password" />
<p:inputText type="password" id="Password2" value="#{userBackingBean.password}" required="true" label="Password2"/>
<p:message for="Password2" />
</h:panelGrid>
</p:panel>
<p:toolbar>
<f:facet name="left">
<p:commandButton value="Registrarse" action="goHome" update="panel" actionListener="#{userClientBean.addUser()}" style="margin-right:40px;"/>
<script>
function cifrar(){
var input_pass = document.getElementById("password");
input_pass.value = sha1(input_pass.value);
}
</script>
</f:facet>
</p:toolbar>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
UserBackingBean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package client;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class UserBackingBean implements Serializable{
String nombre;
String email;
String password;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
UserClientBean
package client;
import com.mycompany.pfinalpsegrupo2.Usuarios;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import json.UserReader;
import json.UserWriter;
@Named
@RequestScoped
public class UserClientBean {
@Inject
UserBackingBean bean;
Client client;
WebTarget target;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
target = client.target("http://localhost:8080/PFinalPSEGrupo2/webresources/com.mycompany.pfinalpsegrupo2.usuarios");
}
@PreDestroy
public void destroy() {
client.close();
}
public Usuarios[] getUsers() {
return target
.request()
.get(Usuarios[].class);
}
public Usuarios getUser() {
return target
.register(UserReader.class)
.path("{Email}")
.resolveTemplate("Email", bean.getEmail())
.request(MediaType.APPLICATION_JSON)
.get(Usuarios.class);
}
//No pone que el usuario pueda darse de baja,método a revisar
public void deleteUser() {
target.path("{Email}")
.resolveTemplate("Email", bean.getEmail())
.request()
.delete();
}
public void addUser() {
Usuarios u = new Usuarios();
u.setEmail(bean.getEmail());
u.setNombre(bean.getNombre());
u.setPassword(bean.password);
target.register(UserWriter.class)
.request()
.post(Entity.entity(u,MediaType.APPLICATION_JSON));
}
}
UserReader
package json;
import com.mycompany.pfinalpsegrupo2.Usuarios;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class UserReader implements MessageBodyReader<Usuarios> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Usuarios.class.isAssignableFrom(type);
}
@Override
public Usuarios readFrom(Class<Usuarios> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
Usuarios usuario = new Usuarios();
JsonParser parser = Json.createParser(entityStream);
while (parser.hasNext()) {
switch (parser.next()) {
case KEY_NAME:
String key = parser.getString();
parser.next();
switch (key) {
case "id":
usuario.setEmail(parser.getString());
break;
case "name":
usuario.setNombre(parser.getString());
break;
case "actors":
usuario.setPassword(parser.getString());
break;
default:
break;
}
break;
default:
break;
}
}
return usuario;
}
}
UserWriter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package json;
import com.mycompany.pfinalpsegrupo2.Usuarios;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class UserWriter implements MessageBodyWriter<Usuarios> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Usuarios.class.isAssignableFrom(type);
}
@Override
public long getSize(Usuarios t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Usuarios t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
JsonGenerator gen = Json.createGenerator(entityStream);
gen.writeStartObject()
.write("Email", t.getEmail())
.write("Nombre", t.getNombre())
.write("Password", t.getPassword())
.writeEnd();
gen.flush();
}
}
Gracias de antemano.
Valora esta pregunta


0