applicationContext-action.xml
---------
<bean id="loginServlet"
class="com.oyulu.servlet.LoginServlet">
<property name="login">
<bean class="com.oyulu.bean.LoginImpl"/>
</property>
</bean>===============================================================
web.xml
-----------
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param> <!--Spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.oyulu.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
===============================================================
LoginServlet.java
-----------
public class LoginServlet extends HttpServlet { private ILogin login ;
public ILogin getLogin() {
return login;
} public void setLogin(ILogin login) {
this.login = login;
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String name = request.getParameter("name");
String password = request.getParameter("password");
// login = new LoginImpl();
boolean isLogin = login.isLogin(name, password);
if(isLogin)
request.getRequestDispatcher("right.html").forward(request, response);
else
request.getRequestDispatcher("false.html").forward(request, response);
}}
===============================================================
LoginImpl .java
-----------
public class LoginImpl implements ILogin { public boolean isLogin(String name, String password) {
System.out.println("name :" + name + "/" + "password :" + password);
if(name.equals("hello") && password.equals("hello"))
return true;
else
return false;
}}
==============================
运行时 抛出java.lang.NullPointerException
com.oyulu.servlet.LoginServlet.doPost(LoginServlet.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)大家看看 错在哪里了 我实在是找不到错在哪里
---------
<bean id="loginServlet"
class="com.oyulu.servlet.LoginServlet">
<property name="login">
<bean class="com.oyulu.bean.LoginImpl"/>
</property>
</bean>===============================================================
web.xml
-----------
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param> <!--Spring的监听器,以便在启动时就自动加载spring的配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.oyulu.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
===============================================================
LoginServlet.java
-----------
public class LoginServlet extends HttpServlet { private ILogin login ;
public ILogin getLogin() {
return login;
} public void setLogin(ILogin login) {
this.login = login;
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String name = request.getParameter("name");
String password = request.getParameter("password");
// login = new LoginImpl();
boolean isLogin = login.isLogin(name, password);
if(isLogin)
request.getRequestDispatcher("right.html").forward(request, response);
else
request.getRequestDispatcher("false.html").forward(request, response);
}}
===============================================================
LoginImpl .java
-----------
public class LoginImpl implements ILogin { public boolean isLogin(String name, String password) {
System.out.println("name :" + name + "/" + "password :" + password);
if(name.equals("hello") && password.equals("hello"))
return true;
else
return false;
}}
==============================
运行时 抛出java.lang.NullPointerException
com.oyulu.servlet.LoginServlet.doPost(LoginServlet.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)大家看看 错在哪里了 我实在是找不到错在哪里
if(name.equals("hello") && password.equals("hello"))
这一句没有判断null的情况
如果
String name = request.getParameter("name");
String password = request.getParameter("password")
是null 那就会报错
-----------
public class LoginImpl implements ILogin {public boolean isLogin(String name, String password) {
System.out.println("name :" + name + "/" + "password :" + password);
if(name.equals("hello") && password.equals("hello"))
return true;
else
return false;
}} 红色部分有问题,如果name或者password为null则报错
name或password为null时当然就报nullpoint错误了,if(name.equals("hello") && password.equals("hello")) 在他们前面加上
if(name != null && password != null)
package com.east.spring.bean;import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author:East(张栋芳)
* @date:Nov 24, 2008
* @content:测试注入属性
*/
public class TestBean {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
//日期格式的注入不同,要写一个属性编辑器来进行转换
private Date dateValue; public String getStrValue() {
return strValue;
}.............................
}
------------------------------------------
package com.east.spring.comm;import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;/**
* @author:East(张栋芳)
* @date:Nov 24, 2008
* @content:用于转换日期格式的属性编辑器
*/public class DatePropertyEditor extends PropertyEditorSupport {
private String format = null; public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date d = sdf.parse(text);
this.setValue(d);
} catch (Exception se) {
se.printStackTrace();
} } public void setFormat(String format) {
this.format = format;
}}
----------------------------------
package com.east.spring.test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.east.spring.bean.TestBean;public class TestInjection {
private static BeanFactory factory = null;
static {
factory = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void display(){
TestBean bean = (TestBean)factory.getBean("testBean");
System.out.println("bean.getStrValue:"+bean.getStrValue());
System.out.println("bean.getIntValue:"+bean.getIntValue());
System.out.println("bean.getListValue:"+bean.getListValue());
System.out.println("bean.getSetValue:"+bean.getSetValue());
System.out.println("bean.getArrayValue:"+bean.getArrayValue());
System.out.println("bean.getMapValue:"+bean.getMapValue());
System.out.println("bean.getDateValue:"+bean.getDateValue());
} public static void main(String[] args) {
TestInjection test = new TestInjection();
test.display();
}}
-----------
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 加入 的配置信息来注入属性 -->
<bean id="testBean" class="com.east.spring.bean.TestBean">
<!-- String类型的 -->
<property name="strValue" value="east" />
<!-- Int类型的 -->
<property name="intValue" value="123" />
<!-- List类型的 -->
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!-- Set类型的 -->
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<!-- String[] 类型的 -->
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
<value>array3</value>
</list>
</property>
<!-- map类型的 -->
<property name="mapValue">
<map>
<entry key="m1" value="v1"></entry>
<entry key="m2" value="v2"></entry>
</map>
</property>
<!-- 日期类型的要写一个属性编辑器来转换 -->
<property name="dateValue">
<value>2008/10/21</value>
</property>
</bean>
<!-- 日期类型的属性编辑器的定义 -->
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.east.spring.comm.DatePropertyEditor">
<!-- 指定注入的格式 格式要与日期的字符串一致-->
<property name="format" value="yyyy/mm/dd"></property>
</bean>
</entry>
</map>
</property>
</bean></beans>
你这样配置,servlet的生成与销毁根本是服务器控制,spring更本生成servlet,更加不会向servlet注入你想要的属性:ILogIn!
这样就应该可以:在你原来基础上,如果你要获取ILogIn实体类时,改用:
doPost方法加上:
login = login = (ILogInDao) WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()).getBean("ILogIn实体类名");
这样就应该可以拉!