action类:
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName;
private String userPwd;
private LoginService lc;
private TbUser user;
public TbUser getU() {
return user;
}
public void setU(TbUser u) {
this.user = u;
}
public String getName() {
return userName;
}
public void setName(String name) {
this.userName = name;
}
public String getPassword() {
return userPwd;
}
public void setPassword(String password) {
this.userPwd = password;
}
public LoginService getLc() {
return lc;
}
public void setLc(LoginService lc) {
this.lc = lc;
}
public String execute() throws Exception {
System.out.print("用户名:"+userName+"密码:"+userPwd);
boolean b = lc.isLogin(user);
if (b) {
ActionContext.getContext().getSession().put("login", "true");
return SUCCESS;
} else {
return ERROR;
}
}
}
applicationContext.xml文件里:
<bean id="loginService" class="com.bus.service.LoginService">
<!-- 为业务逻辑组件注入DAO组件 -->
<property name="dao" ref="TbUserDAO"></property>
</bean>
<!-- 创建LoginAction实例 -->
<bean id="loginAction" class="com.bus.action.LoginAction" scope="prototype">
<property name="lc" ref="loginService">
</property>
</bean>
struts.xml文件:
<package name="myPackage" extends="struts-default">
<interceptors>
<interceptor name="clearFieldErrorInterceptor"
class="com.bus.util.ClearFieldErrorInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="clearFieldErrorInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" class="loginAction">
<result name="success">loginOk.jsp</result>
<result name="input">login.jsp</result>
<result name="error">loginFailure.jsp</result>
</action>
</package>
login.jsp 文件表单:
<form action="login" method="post" name="loginForm">
<h1>
用户登录
</h1>
<input id="userName1" type="text" name="user.userName" placeholder="用户名" />
<input id="userPwd1" type="password" name="user.userPwd" placeholder="密码" />
<input type="submit" name="login" value="登录" onclick="test()" />
<input type="reset" name="reset" value="重置" />
</form>
运行后抛出异常:
java.lang.NullPointerException
com.bus.action.LoginAction.execute(LoginAction.java:56)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)