1、实现功能: 用户输入用户名和密码,当用户名是“test”时,跳转到登录成功界面;若不是,跳转到登录失败界面。
2、第一步:编写login.jsp页面: 输入框为用户名和密码,点击“提交”进行表单提交,action="<%=request.getContextPath()%>/login.do" method=“post”,action的路径是login.do,下一步配置web.xml文件。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/login.do" method="post">
用户名:<input type="text" name="username"></br> <!--此处name与后面form的成员变量相同,否则:警告: Unhandled exception:java.lang.NullPointerException-->
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
3、配置web.xml文件: 对所有url=“*.do”,servlet的那么都为action,其对应的类都是org.apache.struts.action.ActionServlet,此处直接引用了structs的类。接着初始化相关的配置参数:,引入/WEB-INF/struts-config.xml文件对form和action进行配置。
注:myeclispe动态项目下,无法打开struts-config.xml文件,此时出现Project StructsTest is not configured as a MyEclipse Web-Struts Project. Therefore the MyEclipse Struts Editor may not b
错误
解决方案一:在项目下的.project文件里添加一行代码:<nature>com.genuitec.eclipse.cross.easystruts.eclipse.easystrutsnature</nature>
解决方案二:右键单击工程,选择MyEclipse->add Struts capabilities(由于myeclipse版本问题,我并没有找到该选项,该方法并没有实践过。)
为了方便学习,我更换了ide,用eclipse进行开发,eclipse不会出现这个问题。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 跟初始化相关的配置参数 -->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4、配置structs-config.xml文件:
注:<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
若版本写错,会出现**Content is not allowed in prolog.**错误,原因是无法识别,因为导入的是structs1.3.8,需要与声明一致。另当eclipse中一直提示“Content is not allowed in prolog”错误是,先重新编译一遍程序查看错误是否还存在。
<!DOCTYPE struts-config PUBLIC
中 struts-config写成struts也会报错,会出现文档根元素 "struts-config" 必须匹配 DOCTYPE 根 "struts"。
错误。
<?xml version="1.0" encoding="UTF-8" ?>
<!--该声明一定要正确,要不然就出现各种问题-->
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<!--声明一个form-bean,type="当前bean的完整类名,此处的bean用于src下面,故直接为LoginForm"-->
<form-bean name="loginForm" type="LoginForm" ></form-bean>
</form-beans>
<action-mappings>
<!--声明一个action,type="当前action的完整类名,此处的action用于src下面,故直接为LoginAction"-->
<action path="/login" type="LoginAction" name="loginForm">
<!--声明action如何跳转,path为跳转路径"-->
<forward name="loginFailure" path="/loginFailure.jsp"></forward>
<forward name="loginSuccess" path="/loginsuccess.jsp"></forward>
</action>
</action-mappings>
</struts-config>
5、LoginForm.java文件:
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm{
/** fields LoginForm.java
*/
private static final long serialVersionUID = 1L;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
private String username;
}
**6、LoginAction.java:**重写execute方法
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;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse httpservletresponse)
throws Exception {
LoginForm loginform = (LoginForm)form; //强制转换
System.out.println("进入到action");
if(loginform.getUsername().equals("test")){
return mapping.findForward("loginSuccess"); //为‘test’跳转到成功界面
}else{
return mapping.findForward("loginFailure");
}
}
}
7、loginFailure.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登录失败
</body>
</html>
8、loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登录成功
</body>
</html>
9、java.lang.ClassNotFoundException: org.apache.struts.action.ActionServlet
在bin目录下导入structs的包
下载链接:https://download.csdn.net/download/abracadabra__/11025487