Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. public class Login {
  2.  
  3. private String userName;
  4. private String password;
  5. private boolean loggedin;
  6.  
  7. public Login(){};
  8.  
  9. /**
  10. * @return the loggedin
  11. */
  12. public boolean isLoggedin() {
  13. return loggedin;
  14. }
  15.  
  16. /**
  17. * @param loggedin the loggedin to set
  18. */
  19. public void setLoggedin(boolean loggedin) {
  20. this.loggedin = loggedin;
  21. }
  22.  
  23. /**
  24. * @param userName
  25. * @param password
  26. */
  27. public Login(String userName, String password) {
  28. this.userName = userName;
  29. this.password = password;
  30. }
  31.  
  32. /**
  33. * @return the userName
  34. */
  35. public String getUserName() {
  36. return userName;
  37. }
  38.  
  39. /**
  40. * @param userName the userName to set
  41. */
  42. public void setUserName(String userName) {
  43. this.userName = userName;
  44. }
  45.  
  46. /**
  47. * @return the password
  48. */
  49. public String getPassword() {
  50. return password;
  51. }
  52.  
  53. /**
  54. * @param password the password to set
  55. */
  56. public void setPassword(String password) {
  57. this.password = password;
  58. }
  59.  
  60. }
  61.  
  62. @Controller
  63. public class AuthenticationController {
  64.  
  65. private final Logger logger = Logger.getLogger(getClass());
  66.  
  67. private AuthenticationManager authenticationManager;
  68. private Login login = new Login();
  69.  
  70. String message = "Congrulations You Have Sucessfully Login";
  71. String errorMsg = "Login Unsucessful";
  72.  
  73. @RequestMapping(value="login.htm")
  74. public ModelAndView onSubmit(Object command) throws ServletException {
  75.  
  76. String userName = ((Login)command).getUserName();
  77. String password = ((Login)command).getPassword();
  78.  
  79. login.setUserName(userName);
  80. login.setPassword(password);
  81.  
  82. logger.info("Login was set");
  83.  
  84. logger.info("the username was set to " + login.getUserName());
  85. logger.info("the password was set to " + login.getPassword());
  86.  
  87. if (authenticationManager.Authenticate(login) == true){
  88. return new ModelAndView("main","welcomeMessage", message);
  89. }
  90.  
  91. //return new ModelAndView("main","welcomeMessage", message);
  92. return new ModelAndView("login","errorMsg", "Error!!!");
  93. }
  94.  
  95. }
  96.  
  97. <form:form action="yourUrl" modelAttribute="login" method="POST">
  98. <% ... %>
  99. </form:form>
  100.  
  101. // your method that prints the form
  102. public ModelAndView onGet(@ModelAttribute Login login) {
  103. // return ...
  104. }
  105.  
  106. @RequestMapping(value="login.htm")
  107. public ModelAndView onSubmit(@ModelAttribute Login login) {
  108. String userName = login.getUserName();
  109. String password = login.getPassword();
  110. // ...
  111. }
  112.  
  113. // the methods can have the name you want
  114. // not only onGet, onPost, etc. as in servlets
  115.  
  116. @RequestMapping("url1.htm")
  117. public String loadAnyJsp(@ModelAttribute Login login) {
  118. return "path/to/my/views/login";
  119. }
  120.  
  121. @RequestMapping("url2.htm")
  122. public String redirectToAnotherController(@ModelAttribute Login login) {
  123. return "redirect:url1.htm";
  124. }
  125.  
  126. ...
  127. <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:favorPathExtension="true" p:favorParameter="true" p:ignoreAcceptHeader="true" p:defaultContentType="text/html">
  128. <description>Depending on extension, return html with no decoration (.html), json (.json) or xml (.xml), remaining pages are decoracted</description>
  129. <property name="mediaTypes">
  130. <map>
  131. <entry key="xml" value="application/xml" />
  132. <entry key="json" value="application/json" />
  133. <entry key="html" value="text/html" />
  134. <entry key="action" value="text/html" />
  135. </map>
  136. </property>
  137. <property name="defaultViews">
  138. <list>
  139. <bean class="org.springframework.web.servlet.view.xml.MarshallingView" p:marshaller-ref="xstreamMarshaller" />
  140. <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
  141. </list>
  142. </property>
  143. <property name="viewResolvers">
  144. <list>
  145. <bean id="nameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
  146. <description>Maps a logical view name to a View instance configured as a Spring bean</description>
  147. </bean>
  148. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
  149. </list>
  150. </property>
  151. </bean>
  152. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement