Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package com.programcreek.helloworld.controller;
  2.  
  3. import java.sql.SQLException;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.servlet.ModelAndView;
  11.  
  12.  
  13.  
  14. @Controller
  15. public class LoginController {
  16.  
  17. @RequestMapping("/login")
  18. public ModelAndView viewLogin()
  19. {
  20. ModelAndView mv = new ModelAndView("login");
  21. return mv;
  22. }
  23.  
  24.  
  25. @RequestMapping(value ="/submitlogin", method= RequestMethod.POST)
  26. public ModelAndView submituserLogin(@RequestParam("userName")String userName, @RequestParam("password")String password ) throws SQLException
  27. {
  28.  
  29. ApplicationContext context =
  30. new ClassPathXmlApplicationContext("Beans.xml");
  31.  
  32. LicenseManagementDaoImp LicenseManagementDaoImp =
  33. (LicenseManagementDaoImp)context.getBean("LicenseManagementDaoImp");
  34.  
  35. UserAccountController formInfo = new UserAccountController(userName,password);
  36. UserAccountController userAcc = LicenseManagementDaoImp.loginInfo(userName);
  37.  
  38. if(formInfo.getUserName() == userAcc.getUserName() && formInfo.getPassword() == userAcc.getPassword())
  39. {
  40. ModelAndView mv = new ModelAndView("loginsuccess");
  41. mv.addObject("headermsg","You have successfully logged in");
  42. mv.addObject("msg1", userAcc.getUserName());
  43. mv.addObject("msg2", userAcc.getPassword());
  44. return mv;
  45. }
  46.  
  47. else
  48. {
  49. ModelAndView mv = new ModelAndView("login");
  50. mv.addObject("msg1", "Please enter a Valid Username or Password");
  51. return mv;
  52.  
  53. }
  54.  
  55.  
  56. }
  57.  
  58. }
  59.  
  60. package com.programcreek.helloworld.controller;
  61.  
  62. public class UserAccountController
  63.  
  64. {
  65. private String userName;
  66. private String password;
  67.  
  68. public UserAccountController()
  69. {
  70.  
  71. }
  72.  
  73. public UserAccountController(String userName, String password)
  74. {
  75. super();
  76. this.userName = userName;
  77. this.password = password;
  78. }
  79.  
  80. public String getUserName() {
  81. return userName;
  82. }
  83.  
  84. public void setUserName(String userName) {
  85. this.userName = userName;
  86. }
  87.  
  88. public String getPassword() {
  89. return password;
  90. }
  91.  
  92. public void setPassword(String password) {
  93. this.password = password;
  94. }
  95.  
  96.  
  97. }
  98.  
  99. package com.programcreek.helloworld.controller;
  100.  
  101. import java.sql.SQLException;
  102. import javax.sql.DataSource;
  103. import org.springframework.dao.EmptyResultDataAccessException;
  104. import org.springframework.jdbc.core.JdbcTemplate;
  105.  
  106. public class LicenseManagementDaoImp
  107. {
  108. private DataSource dataSource;
  109. private JdbcTemplate jdbcTemplateObject;
  110.  
  111. public void setDataSource(DataSource dataSource)
  112. {
  113. this.dataSource = dataSource;
  114. this.jdbcTemplateObject = new JdbcTemplate(dataSource);
  115. }
  116.  
  117.  
  118. public UserAccountController loginInfo(String userName) throws SQLException
  119. {
  120. try
  121. {
  122. String SQL = "Select strusername, strpassword from user where strusername = ? ";
  123. UserAccountController userAcc = jdbcTemplateObject.queryForObject(SQL, new LicenseManagementMapper(), userName);
  124. return userAcc;
  125.  
  126. }
  127. catch(EmptyResultDataAccessException e)
  128. {
  129. return new UserAccountController();
  130. }
  131. }
  132.  
  133.  
  134.  
  135. }
  136.  
  137. package com.programcreek.helloworld.controller;
  138.  
  139. import java.sql.ResultSet;
  140. import java.sql.SQLException;
  141. import org.springframework.jdbc.core.RowMapper;
  142.  
  143.  
  144.  
  145. public class LicenseManagementMapper implements RowMapper<UserAccountController>
  146. {
  147. public UserAccountController mapRow(ResultSet rs, int rowNum) throws SQLException
  148. {
  149. UserAccountController userAcc = new UserAccountController();
  150. userAcc.setUserName(rs.getString("strusername"));
  151. userAcc.setPassword(rs.getString("strpassword"));
  152. return userAcc;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement