搭建Struts框架工具:
Eclipse
Tomcat
如果没有在eclipse里面安装Toncat插件的同学可以参考这篇文章
https://blog.csdn.net/weixin_44003632/article/details/86629997
首先,我们需要下载关于框架的jar包,该jar包我已经上传到CSDN了,大家可以下载
https://download.csdn.net/download/weixin_44003632/10938170
jar包结构如下
然后我们需要建立JAVA项目,项目结构如下
需要建立两个包,两个JAVA类,一个struts-config.xml文件和一个web.xml文件以及三个.jsp页面。
建立完成以后,接下来就可以上代码了
LoginAction.java
package com.lanp.webapp.action;
import java.util.ArrayList;
import java.util.List;
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 org.apache.struts.action.SessionActionMapping;
import com.lanp.webapp.form.LoginActionForm;
import com.sun.jmx.remote.util.Service;
/**
* author: Sakura
* function:LoginFunction
* date:2018-12-04
*modify date,modify author,modify cause:
*/
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm from,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.print("成功进入!");
String path = "";
LoginActionForm loginActionForm = (LoginActionForm) from;
//String userName = loginActionForm.getName();
//String userpwd = loginActionForm.getPassword();
String Name =request.getParameter("name");
String Pwd=request.getParameter("password");
System.out.println(Name);
System.out.print(Pwd);
if ("111".equals(Name) && "111".equals(Pwd)) {
path = "success";
LoginActionForm p1 = new LoginActionForm();
p1.setName("孤傲苍狼");
p1.setPassword("12345");
LoginActionForm p2 = new LoginActionForm();
p2.setName("白虎神皇");
p2.setPassword("55555");
List<LoginActionForm> list = new ArrayList<LoginActionForm>();
list.add(p1);
list.add(p2);
request.setAttribute("list",list);
request.setAttribute("name", Name);
} else {
path = "error";
}
return mapping.findForward(path);
}
/*
* @Override public ActionForward execute(ActionMapping mapping, ActionForm
* form, HttpServletRequest request, HttpServletResponse response) throws
* Exception { String path = "error"; LoginActionForm loginActionForm =
* (LoginActionForm)form; String userName = loginActionForm.getName();
* String passWord = loginActionForm.getPassword();
*
* if("admin".equals(userName) && "admin".equals(passWord)) { path =
* "success"; request.setAttribute("name", userName); } else { path =
* "error"; } return mapping.findForward(path); }
*/
}
LoginActionForm.java
package com.lanp.webapp.form;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm{
private String name;
private String password;
private String sex;
private Integer age;
@Override
public String toString() {
return "LoginActionForm [name=" + name + ", password=" + password
+ ", sex=" + sex + ", age=" + age + "]";
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(int i) {
this.age = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
author: Sakura
funtion: struts connection
date: 2018-12-7
modify date,modify author,modify cause
-->
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<!-- name是ActionForm的名字,是唯一标示。在action中会用到,是关联action的唯一标示。 -->
<!-- type是ActionForm所在路径,是由包名+类名组成。 -->
<form-bean name="LoginActionForm" type="com.lanp.webapp.form.LoginActionForm">
</form-bean>
<form-bean name="IndexActionForm" type="com.lanp.webapp.form.IndexActionForm">
</form-bean>
</form-beans>
<action-mappings>
<!-- path是Login.jsp中表单action提交的地址,也就是将要访问的action资源。
name是action的名字,用来关联某个表单,是唯一标示。
type是action的路径,由包名+类名组成。
scope表示ActionForm存在的范围,有session和request两种,默认是session。
forward是路径跳转 -->
<action path="/Login" name="LoginActionForm" type="com.lanp.webapp.action.LoginAction" scope="request">
<forward name="login" path="/Login.jsp"></forward>
<forward name="success" path="/LoginSuccess.jsp"></forward>
<forward name="error" path="/LoginFailed.jsp"></forward>
</action>
<action path="/Index" name="LoginActionForm" type="com.lanp.webapp.action.TestAjax2Action" scope="request">
<forward name="Test" path="/Test.jsp"></forward>
</action>
</action-mappings>
</struts-config>
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsDemo</display-name>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<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/conf/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>
Login.jsp
<!--
author:Sakura
function:Login
date:2018-12-04
modify date,modify author,modify cause:
-->
<%@ 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>
<%
String basePath = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function Index()
{
if (name.value!=null && password.value!=null) {
form1.action = "Login.do?username="+form1.name.value+"&&password="+form1.password.value+"";
form1.submit();
alert("OK Come On");
}
else {
alert(name+password+"please select accounting rule type! ");
return false;
}
}
function Index(){
window.location="index.jsp";
}
</script>
</head>
<body>
<h1>登陆页面</h1>
<hr>
<form name="form1" action="<%=basePath %>/Login.do" method="post" >
userName:<input id="name" name="name" type="text" nullMsg="nimade "/><br>
passWord:<input id="password" name="password" type="password" nullMsg="fuckyou" /><br>
<!-- <input type="submit" id="submit" name="submit" value="submit" /> -->
<input type="submit" name="Submit25" value="Login" class="button" onClick="Index()">
<!-- <input type="button" name="Submit25" value="indexList" class="button" onClick="Index()"> -->
</form>
</body>
</html>
LoginFailed.jsp
<!--
author:Sakura
function:LoginFailed in the Jsp
date:2018-12-04
modify date,modify author,modify cause:
-->
<%@ 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>登录失败</title>
</head>
<body>
<h1>登录失败!</h1>
请返回<a href="/StrutsDemo/Login.jsp">登录页面。</a>
</body>
</html>
LoginSuccess.jsp
<!--
author:Sakura
function:Login success in the Jsp
date:2018-12-04
modify date,modify author,modify cause:
-->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.*"%>>
<%@page import="java.util.ArrayList"%>
<%@ page import="com.lanp.webapp.form.LoginActionForm"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ page isELIgnored="false"%>
<!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>登录成功</title>
</head>
<body>
<h1>登录成功页面!OKOK</h1>
<h3>
欢迎[<%=request.getAttribute("name")%>]
</h3>
<span style="margin-left: 50px; font-size: 20px;"> 渠道来源:<select
id="channelname" name="channelname" cssClass="input4">
<c:forEach var="value" items="${list}">
<option value="value">${value}</option>
</c:forEach>
</select>
</span>
<%
ArrayList list = (ArrayList) request.getAttribute("list");
/* 接受list集合 */
%>
<table>
<tr>
<td>姓名</td>
<td>密码</td>
</tr>
<%
for (int i = 0; i < list.size(); i++) {
LoginActionForm pl = (LoginActionForm) list.get(i);
/* LoginActionFrom是你的实体类 */
%>
<td><%=pl.getName()%></td>
<td><%=pl.getPassword()%></td>
<tr>
<%
}
%>
</tr>
el方法:
<c:forEach var="person" items="${list}">
${list[1].name}
<tr>
<td>${person.name}</td>
<td>${person.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
导入代码以后运行Tomcat软件
然后在网页里面输入你的路径,就成功了