Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4. xmlns:h="http://xmlns.jcp.org/jsf/html"
  5. xmlns:p="http://primefaces.org/ui">
  6. <h:head>
  7. <title>Dezurstvo</title>
  8. </h:head>
  9. <h:body>
  10. Login
  11. <br />
  12. <h:form>
  13. <p:growl id="msgs" showDetail="true"/>
  14. <h:panelGrid columns="2" cellpadding="5">
  15. <h:outputText value="Username"/>
  16. <p:inputText value="#{logIn.username}" required="true"/>
  17. <h:outputText value="Password"/>
  18. <p:password id="pass" value="#{logIn.password}" feedback="false" required="true"/>
  19. </h:panelGrid>
  20. <p:commandButton value="Login" action="#{logIn.loadUser()}" update="msgs" />
  21. <p:commandButton value="Regisracija" />
  22. </h:form>
  23. <h:link outcome="adminMain" value="Primefaces welcome page" />
  24. </h:body>
  25. </html>
  26.  
  27. package beans;
  28.  
  29. import dataBeans.Korisnik;
  30. import javax.faces.bean.ManagedBean;
  31. import javax.faces.bean.RequestScoped;
  32. import javax.faces.context.FacesContext;
  33. import javax.servlet.http.HttpSession;
  34.  
  35.  
  36. @ManagedBean(name = "navigation")
  37. @RequestScoped
  38. class NavigationBean {
  39. public String getHomepage(){
  40. FacesContext context = FacesContext.getCurrentInstance();
  41. HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
  42. Korisnik kor = (Korisnik)session.getAttribute("username");
  43. if(!"A".equals(kor.getStatus()) || !"D".equals(kor.getStatus()) || !"N".equals(kor.getStatus()))
  44. return "index";
  45. else if("A".equals(kor.getStatus()))
  46. return "adminMain";
  47. else if("D".equals(kor.getStatus()))
  48. return "demonstratorMain";
  49. else if("N".equals(kor.getStatus()))
  50. return "nastavnikMain";
  51. return "";
  52. }
  53. public static String redirect(String status){
  54. if(!"A".equals(status) || !"D".equals(status) || !"N".equals(status))
  55. return "index";
  56. else if("A".equals(status))
  57. return "adminMain";
  58. else if("D".equals(status))
  59. return "demonstratorMain";
  60. else if("N".equals(status))
  61. return "nastavnikMain";
  62. return "error";
  63. }
  64. }
  65.  
  66. package beans;
  67.  
  68. import dataBeans.Korisnik;
  69. import javax.faces.application.FacesMessage;
  70. import javax.faces.bean.ManagedBean;
  71. import javax.faces.bean.RequestScoped;
  72. import javax.faces.component.UIComponent;
  73. import javax.faces.context.FacesContext;
  74. import javax.faces.event.ActionEvent;
  75. import javax.servlet.http.HttpSession;
  76.  
  77. @ManagedBean(name="logIn")
  78. @RequestScoped
  79. public class LoginBean {
  80. private String user;
  81. private String pass;
  82.  
  83. private UIComponent component;
  84. public UIComponent getComponent() {
  85. return component;
  86. }
  87. public void setComponent(UIComponent component) {
  88. this.component = component;
  89. }
  90.  
  91. public LoginBean() {}
  92.  
  93. /**
  94. * @return the user
  95. */
  96. public String getUsername() {
  97. return user;
  98. }
  99.  
  100. /**
  101. * @param username the user to set
  102. */
  103. public void setUsername(String username) {
  104. this.user = username;
  105. }
  106.  
  107. /**
  108. * @return the pass
  109. */
  110. public String getPassword() {
  111. return pass;
  112. }
  113.  
  114. /**
  115. * @param password the pass to set
  116. */
  117. public void setPassword(String password) {
  118. this.pass = password;
  119. }
  120.  
  121.  
  122. //Loading user into session
  123. public String loadUser() {
  124. FacesContext context = FacesContext.getCurrentInstance();
  125. HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
  126. FacesMessage msg;
  127. Korisnik kor = Korisnik.getUser(user);
  128.  
  129. if (user.equals(kor.getUsername())) {
  130. System.out.println( "user je = " + kor.getUsername());
  131. //User exists in database
  132. if (pass.equals(kor.getPassword())) {
  133. System.out.println( "Sifra je = " + kor.getPassword());
  134. System.out.println( "Status = " + kor.getStatus());
  135. //Pasword is OK
  136. session.setAttribute(user, kor);
  137. return NavigationBean.redirect(kor.getStatus());
  138. } else {
  139. //Wrong pass
  140. // System.out.println( "Sifra je = " + kor.getPassword());
  141. msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogrešna šifra.");
  142. return msg.toString();
  143. }
  144. } else {
  145. //Wrog user
  146. msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Korisnik sa datim korisničkim imenom ne postoji.");
  147. return msg.toString();
  148. }
  149. }
  150.  
  151. public String signOut(){
  152. FacesContext context = FacesContext.getCurrentInstance();
  153. HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
  154. session.invalidate();
  155. return "logIn";
  156. }
  157. }
  158.  
  159. <?xml version='1.0' encoding='UTF-8'?>
  160. <faces-config version="2.2"
  161. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  162. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  163. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
  164.  
  165. <navigation-rule>
  166. <from-view-id>/index.xhtml</from-view-id>
  167. <navigation-case>
  168. <from-outcome>adminMain</from-outcome>
  169. <to-view-id>admin/adminMain.xhtml</to-view-id>
  170. </navigation-case>
  171. <navigation-case>
  172. <from-outcome>demonstratorMain</from-outcome>
  173. <to-view-id>demonstrator/demonstratorMain.xhtml</to-view-id>
  174. </navigation-case>
  175. <navigation-case>
  176. <from-outcome>nastavnikMain</from-outcome>
  177. <to-view-id>nastavnik/nastavnikMain.xhtml</to-view-id>
  178. </navigation-case>
  179. <navigation-case>
  180. <from-outcome>error</from-outcome>
  181. <to-view-id>error.xhtml</to-view-id>
  182. </navigation-case>
  183. </navigation-rule>
  184. </faces-config>
  185.  
  186. <?xml version="1.0" encoding="UTF-8"?>
  187. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
  188. <context-param>
  189. <param-name>javax.faces.PROJECT_STAGE</param-name>
  190. <param-value>Development</param-value>
  191. </context-param>
  192. <servlet>
  193. <servlet-name>Faces Servlet</servlet-name>
  194. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  195. <load-on-startup>1</load-on-startup>
  196. </servlet>
  197. <servlet-mapping>
  198. <servlet-name>Faces Servlet</servlet-name>
  199. <url-pattern>/faces/*</url-pattern>
  200. </servlet-mapping>
  201. <session-config>
  202. <session-timeout>
  203. 30
  204. </session-timeout>
  205. </session-config>
  206. <welcome-file-list>
  207. <welcome-file>faces/index.xhtml</welcome-file>
  208. </welcome-file-list>
  209.  
  210. </web-app>
  211.  
  212. Warning: JSF1091: No mime type could be found for file /javax.faces.application.FacesMessage@67cc4160. To resolve this, add a mime-type mapping to the applications web.xml.
  213. Warning: JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement