- 创建web工程
2.添加strutslib库jar文件
3.创建struts配置文件struts-config.xml
4.在web.xml中配置struts-config配置文件及配置actionservlet
'''
<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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
''
5.配置struts-config文件在配置文件中同时声明forward跳转页面配置及form关联配置
'''
<struts-config>
<form-beans>
<form-bean name="SForm" type="com.scm.auth.action.Forminfo" />
</form-beans>
<action-mappings>
<action path="/login" type="com.scm.auth.action.loginAction" name="SForm">
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/failure.jsp" />
</action>
</action-mappings>
</struts-config>
'''
实例代码如下:
1 前台代码
登录页面
'''
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page.
</body>
</html>
'''
成功页面
'''
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ok.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登录成功!!
</body>
</html>
'''
失败页面
'''
<body>
登录失败!!
</body>
</html>
'''
2前台页面设计完毕后新建逻辑处理类,继承于Action并重写excute方法。
2.1首先配置struts-config文件正确配置forword和formbeans
配置示例如下:
'''
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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 name="SForm" type="com.scm.auth.action.Forminfo" />
</form-beans>
<action-mappings>
<action path="/login" type="com.scm.auth.action.loginAction" name="SForm">
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/failure.jsp" />
</action>
</action-mappings>
</struts-config>
'''
login处理类代码实现
'''
package com.scm.auth.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;
public class loginAction extends Action{
//重写执行函数
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Forminfo lform =(Forminfo)form;
String username = lform.getUsername();
String passwd = lform.getPasswd();
if(username != null && username.equals("admin")){
return mapping.findForward("success");
}else {
return mapping.findForward("failure");
}
//return super.execute(mapping, form, request, response);
}
}
'''
form-bean类实现
'''
package com.scm.auth.action;
import org.apache.struts.action.ActionForm;
public class Forminfo extends ActionForm{
String username;
String passwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
'''
网友评论