Update ResourcesControllerAdd.java
This commit is contained in:
parent
447d69ce89
commit
1fac144c9a
@ -1,15 +1,73 @@
|
||||
package controller.resources;
|
||||
|
||||
import javax.jdo.JDOObjectNotFoundException;
|
||||
import javax.jdo.PersistenceManager;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class ResourcesControllerAdd extends HttpServlet {
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
||||
PersistenceManager pm = controller.PMF.get().getPersistenceManager();
|
||||
|
||||
//Accion a realizar
|
||||
String action = request.getParameter("action");
|
||||
|
||||
if (action == null)
|
||||
action = "";
|
||||
|
||||
switch (action){
|
||||
//Crea
|
||||
case "create":
|
||||
|
||||
String url = request.getParameter("url");
|
||||
Boolean status = Boolean.parseBoolean(request.getParameter("status"));
|
||||
|
||||
Resource resource = new Resource(url,status);
|
||||
|
||||
try{
|
||||
pm.makePersistent(resource);
|
||||
} finally {
|
||||
System.out.println("Recurso creado");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "redirect":
|
||||
HttpSession sesion= request.getSession();
|
||||
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/View/Resources/add.jsp");
|
||||
request.setAttribute("User",UsersControllerView.getUser(sesion.getAttribute("userID").toString()));
|
||||
dispatcher.forward(request, response);
|
||||
break;
|
||||
|
||||
case "update":
|
||||
|
||||
Key a = KeyFactory.stringToKey(request.getParameter("key"));
|
||||
|
||||
Resource resource = pm.getObjectById(Resource.class, a);
|
||||
|
||||
resource.setName(request.getParameter("url"));
|
||||
resource.setStatus(Boolean.parseBoolean(request.getParameter("status")));
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
pm.close();
|
||||
try{
|
||||
response.sendRedirect("/resource");
|
||||
}
|
||||
//Al redirigr al jsp para crear, se usa RequestDispatcher, y este entra en conflicto con sendRedirect.
|
||||
catch (IllegalStateException e){
|
||||
System.err.println("IllegalStateException: There was a double redirect.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
|
Loading…
Reference in New Issue
Block a user