Spring.xml
创新互联是一家专注于成都网站设计、网站制作和成都移动服务器托管的网络公司,有着丰富的建站经验和案例。
<util:properties id="db" location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.xms" />
<mvc:annotation-driven />
<!-- 声明DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{db.driver}" />
<property name="url" value="#{db.url}" />
<property name="username" value="#{db.user}"/>
<property name="password" value="#{db.password}"/>
</bean>
<!-- 导入SpringJDBC -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 引入数据源 -->
<!--
<property name="dataSource" ref="dataSource"></property>
-->
<constructor-arg>
<ref bean="dataSource"/>
</constructor-arg>
</bean>
<!-- 实例化dao和service接口 -->
<bean id="loginDao" class="com.xms.dao.LoginDaoImpl">
<!-- 注入Spring的jdbcTemplate -->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="loginService" class="com.xms.service.loginServiceImpl">
<property name="loginDao" ref="loginDao"></property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
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{
//该行报错 loginService为null
User u = loginService.checkInfo(user);
HttpSession session = request.getSession();
session.setAttribute("nickname", u.getNickname());
return "index";
}
}
dao接口:
public interface LoginDao {
//根据email查找User
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<User>) new User(), params);
return u;
}
}
service接口:
@Service
public interface LoginService {
public User checkInfo(User user) throws Exception;
}
service实现类:
@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){
//用户名错误
throw new EmailErrorException("用户名错误");
}else if(!u.getPassword().equals(user.getPassword())){
//密码错误
throw new PasswordErrorException("密码错误");
}else{
//用户名和密码正确
return u;
}
}
}
当前名称:Spring注入Bean时为null
浏览路径:/article22/jgcjjc.html
成都网站建设公司_创新互联,为您提供关键词优化、标签优化、网站营销、App设计、网站设计、网站策划
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联