Advertisement
Guest User

Untitled

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