Getting Started with Struts Struts framework to build a simple zero-based learning Struts

Struts framework to build tools:
the Eclipse
Tomcat
students if not in eclipse plug-ins can be installed inside Toncat refer to this article
https://blog.csdn.net/weixin_44003632/article/details/86629997

First, we need to download jar package on the frame of the jar package I have uploaded to the CSDN, you can download
https://download.csdn.net/download/weixin_44003632/10938170
jar package is structured as follows
Here Insert Picture Description
and then we need to build JAVA project, The project structure follows
Here Insert Picture Description
the need to establish two packages, two JAVA classes, a struts-config.xml file and a web.xml file and three .jsp page.

After the establishment of complete, then the code can be more than
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>

After importing the software code running Tomcat
Here Insert Picture Description
and enter your path in the inside pages will be successful
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44003632/article/details/86647081