Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Null when Spring is injected into Bean

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Spring.xml

Jdbc.properties

Driver=com.mysql.jdbc.Driver

Url=jdbc:mysql://127.0.0.1:3306/xmscode?useUnicode=true&characterEncoding=utf8

User=root

Password=1234

Controller:

@ Controller

@ RequestMapping ("login")

Public class LoginController {

Private LoginService loginService

Public LoginService getLoginService () {

Return loginService

}

Public void setLoginService (LoginService loginService) {

This.loginService = loginService

}

@ RequestMapping ("tologin")

Public String toLogin () {

Return "login"

}

@ RequestMapping ("login")

Public String login (User user, HttpServletRequest request) throws Exception {

/ / the line reported an error loginService as null

User u = loginService.checkInfo (user)

HttpSession session = request.getSession ()

Session.setAttribute ("nickname", u.getNickname ())

Return "index"

}

}

Dao interface:

Public interface LoginDao {

/ / find User according to email

Public User findUserByEmail (User user) throws Exception

}

DaoImpl:

@ Repository

Public class LoginDaoImpl implements LoginDao {

Private JdbcTemplate jdbcTemplate

Public JdbcTemplate getJdbcTemplate () {

Return jdbcTemplate

}

Public void setJdbcTemplate (JdbcTemplate jdbcTemplate) {

This.jdbcTemplate = jdbcTemplate

}

Public User findUserByEmail (User user) throws Exception {

User u = null

String sql = "select * from xc_user where email=?"

Object [] params = new Object [] {user.getEmail ()}

U = jdbcTemplate.queryForObject (sql, (RowMapper) new User (), params)

Return u

}

}

Service interface:

@ Service

Public interface LoginService {

Public User checkInfo (User user) throws Exception

}

Service implementation class:

@ Service

Public class loginServiceImpl implements LoginService {

Private LoginDao loginDao

Public LoginDao getLoginDao () {

Return loginDao

}

Public void setLoginDao (LoginDao loginDao) {

This.loginDao = loginDao

}

Public User checkInfo (User user) throws Exception {

System.out.println ("loginDao" + loginDao)

User u = null

U = loginDao.findUserByEmail (user)

If (u==null) {

/ / user name error

Throw new EmailErrorException ("user name error")

} else if (! u.getPassword (). Equals (user.getPassword () {

/ / incorrect password

Throw new PasswordErrorException ("password error")

} else {

/ / user name and password are correct

Return u

}

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report