主要页面
Action: LoginAction.java //用于处理登陆这个事件
FromBean: LoginForm.java //存储Login.jsp中传过来的表单内容
JSP:Login.jsp //登陆页面,提交到login.do
XML配置文件:struts-config.xml,web.xml
struts-config.xml配置文件:
1 <struts-config>
2 <form-beans>
3 <form-bean name="loginForm" type="cn.strut.form.LoginForm" />
4 </form-beans>
5 <action-mappings>
6 <action path="/login" type="cn.strut.action.LoginAction" name="loginForm">
7
8 <forward name="LoginSuccess" path="/LoginSuccess.jsp"></forward>
9 <forward name="LoginFailure" path="/LoginFailure.jsp"></forward>
10 </action>
11 </action-mappings>
12 </struts-config>
LoginAction.java
package cn.strut.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.strut.form.LoginForm;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
LoginForm Lf=(LoginForm)form;
System.out.println(Lf.getUserName()); //每次都输出null
if("AgoniShao".equals(Lf.getUserName()))
{
return mapping.findForward("LoginSuccess");
}
else
{
return mapping.findForward("LoginFailure");
}
}
}
LoginForm.java
package cn.strut.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private String UserName;
private String PassWord;
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassWord() {
return PassWord;
}
public void setPassWord(String passWord) {
PassWord = passWord;
}
}
Login.jsp
<body>
<form action="<%=request.getContextPath() %>/login.do" method="post">
UserName:<input type="text" name="UserName" /><br>
PassWord:<input type="text" name="PassWord" /><br>
<input type="submit" value="OK"/>
</form>
</body>
web.xml
1 <servlet>
2 <servlet-name>action</servlet-name>
3 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
4 <init-param>
5 <param-name>config</param-name>
6 <param-value>/WEB-INF/struts-config.xml</param-value>
7 </init-param>
8 <load-on-startup>0</load-on-startup>
9 </servlet>
10 <servlet-mapping>
11 <servlet-name>action</servlet-name>
12 <url-pattern>*.do</url-pattern>
13 </servlet-mapping>
已知在语法,拼写以及服务器配置方面都是对的,jar包什么的也都正确导入的,但是调试时候会发现无论怎样,运行到LoginAction的时候 LoginForm Lf=(LoginForm)form; 这里的Lf里东西都是空的,即Lf.getUserName()的值都是null 。
感兴趣的朋友可以想想这是什么原因。