JSP页面<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>请登录</title>
</head>
<body>
<html:form action="login">
<html:hidden property="o" value="doLogin" /><br />
<table align = "center">
<tr>
<td width="70">用户名:</td>
<td><html:text property="item.name" style="width:150px" /></td>
</tr>
<tr>
<td>密 码:</td>
<td><html:password property="item.password" style="width:150px" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="登陆"></html:submit></td>
</tr>
</table>
<br />
</html:form>
</body>
</html>
------------------------------------------------------------------
struts 配置文件
-------------------------------------------------------------------
<struts-config>
<data-sources />
<form-beans>
<form-bean name="userInfoForm" type="liupengpo.accountbook.form.UserInfoForm" />
</form-beans>
<action
attribute="userInfoForm"
input="/form/userInfo.jsp"
name="userInfoForm"
path="/login"
scope="request"
parameter="o"
type="liupengpo.accountbook.action.UserInfoAction">
<forward name="toLogin" path="/WEB-INF/login/login.jsp"></forward>
<forward name="billList" path="/WEB-INF/login/billList.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="liupengpo.accountbook.struts.ApplicationResources" />
</struts-config>------------------------------------------------------------------------------------
spring 配置文件
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=AccountBook">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
liupengpo/accountbook/entity/Income.hbm.xml
</value>
<value>
liupengpo/accountbook/entity/Type.hbm.xml
</value>
<value>
liupengpo/accountbook/entity/UserInfo.hbm.xml
</value>
</list>
</property>
</bean>
<!-- 数据访问层 -->
<bean id="userInfoDAO"
class="liupengpo.accountbook.dao.impl.UserInfoDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 业务逻辑层 -->
<bean id="userInfoBiz" class="liupengpo.accountbook.biz.impl.UserInfoBizImpl">
<property name="userInfoDAO">
<ref bean="userInfoDAO"/>
</property>
</bean>
<!-- 表示层 -->
</beans>
---------------------------------------------------------------------
数据访问层
--------------------------------------------------------------------
package liupengpo.accountbook.dao.impl;import java.util.List;import liupengpo.accountbook.dao.UserInfoDAO;
import liupengpo.accountbook.entity.UserInfo;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
*
*/public class UserInfoDAOImpl extends HibernateDaoSupport implements UserInfoDAO {
public List findByName(Object name) {
return findByProperty(NAME, name);
}
}------------------------------------------------------------------------------------
业务逻辑层
-------------------------------------------------------------------------------------
package liupengpo.accountbook.biz.impl;import java.util.List;import liupengpo.accountbook.biz.UserInfoBiz;
import liupengpo.accountbook.dao.UserInfoDAO;
import liupengpo.accountbook.entity.UserInfo;public class UserInfoBizImpl implements UserInfoBiz {
UserInfoDAO userInfoDAO = null;
public boolean login(String name,String password) {
List list = userInfoDAO.findByName(name);
UserInfo userInfo = (UserInfo)list.get(0);
if (userInfo.getPassword().equals(password)) {
return true;
}else{
return false;
}
}
public UserInfoDAO getUserInfoDAO() {
return userInfoDAO;
} public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
this.userInfoDAO = userInfoDAO;
}
}------------------------------------------------------------------------------------------------点击登录后错误如下 调试发现JSP页面中的用户名密码都已经得到--------- 严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException at liupengpo.accountbook.action.UserInfoAction.doLogin(UserInfoAction.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>请登录</title>
</head>
<body>
<html:form action="login">
<html:hidden property="o" value="doLogin" /><br />
<table align = "center">
<tr>
<td width="70">用户名:</td>
<td><html:text property="item.name" style="width:150px" /></td>
</tr>
<tr>
<td>密 码:</td>
<td><html:password property="item.password" style="width:150px" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit value="登陆"></html:submit></td>
</tr>
</table>
<br />
</html:form>
</body>
</html>
------------------------------------------------------------------
struts 配置文件
-------------------------------------------------------------------
<struts-config>
<data-sources />
<form-beans>
<form-bean name="userInfoForm" type="liupengpo.accountbook.form.UserInfoForm" />
</form-beans>
<action
attribute="userInfoForm"
input="/form/userInfo.jsp"
name="userInfoForm"
path="/login"
scope="request"
parameter="o"
type="liupengpo.accountbook.action.UserInfoAction">
<forward name="toLogin" path="/WEB-INF/login/login.jsp"></forward>
<forward name="billList" path="/WEB-INF/login/billList.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="liupengpo.accountbook.struts.ApplicationResources" />
</struts-config>------------------------------------------------------------------------------------
spring 配置文件
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=AccountBook">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
liupengpo/accountbook/entity/Income.hbm.xml
</value>
<value>
liupengpo/accountbook/entity/Type.hbm.xml
</value>
<value>
liupengpo/accountbook/entity/UserInfo.hbm.xml
</value>
</list>
</property>
</bean>
<!-- 数据访问层 -->
<bean id="userInfoDAO"
class="liupengpo.accountbook.dao.impl.UserInfoDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 业务逻辑层 -->
<bean id="userInfoBiz" class="liupengpo.accountbook.biz.impl.UserInfoBizImpl">
<property name="userInfoDAO">
<ref bean="userInfoDAO"/>
</property>
</bean>
<!-- 表示层 -->
</beans>
---------------------------------------------------------------------
数据访问层
--------------------------------------------------------------------
package liupengpo.accountbook.dao.impl;import java.util.List;import liupengpo.accountbook.dao.UserInfoDAO;
import liupengpo.accountbook.entity.UserInfo;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
*
*/public class UserInfoDAOImpl extends HibernateDaoSupport implements UserInfoDAO {
public List findByName(Object name) {
return findByProperty(NAME, name);
}
}------------------------------------------------------------------------------------
业务逻辑层
-------------------------------------------------------------------------------------
package liupengpo.accountbook.biz.impl;import java.util.List;import liupengpo.accountbook.biz.UserInfoBiz;
import liupengpo.accountbook.dao.UserInfoDAO;
import liupengpo.accountbook.entity.UserInfo;public class UserInfoBizImpl implements UserInfoBiz {
UserInfoDAO userInfoDAO = null;
public boolean login(String name,String password) {
List list = userInfoDAO.findByName(name);
UserInfo userInfo = (UserInfo)list.get(0);
if (userInfo.getPassword().equals(password)) {
return true;
}else{
return false;
}
}
public UserInfoDAO getUserInfoDAO() {
return userInfoDAO;
} public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
this.userInfoDAO = userInfoDAO;
}
}------------------------------------------------------------------------------------------------点击登录后错误如下 调试发现JSP页面中的用户名密码都已经得到--------- 严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException at liupengpo.accountbook.action.UserInfoAction.doLogin(UserInfoAction.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
你先debug jsp页面调试下,看看jsp页面中是哪一行出错的,NullPointerException,应该是你调用一个对象的方法,而那个对象却为空,所以出错了。建议在出错的那行前面二三行多看看!
看看什么对象是空指针