快乐编程-知识分享-Struts1.x学习-1.2入门-001

目标:运行第一个struts1.x程序(struts1.2)

对象:适合自学者、初学者、兴趣爱好者。

说明:确保案例代码完整可运行

建议:初学框架,最好用Eclipse(无框架插件),秒杀MyEclipse(有框架插件),框架插件是把双刃剑,优点:入门上手快,代码生产率高,缺点:入门不利于理解框架流程和思路,IDE工具依赖性强。

理念:先行动(Coding),后理解(Thinking)在最短的时间内采取最大量的Coding分享越多,收获越大

--------------------------------------------------------------------------------------------------------------------------------

工程截图:

快乐编程-知识分享-Struts1.x学习-1.2入门-001


一、导入相关包,具体如下截图(struts1.2 lib)

快乐编程-知识分享-Struts1.x学习-1.2入门-001

 

二、配置工程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" 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">
	<display-name>javaee-struts1.2-start</display-name>

	<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/strutsConfig.xml</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</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>

	<welcome-file-list>
		<welcome-file>raky/train/login/login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

 三、编写LoginForm.java类

package com.raky.train.login;

import org.apache.struts.action.ActionForm;
/**
 * struts1.x ActionForm
 * @author raky
 * @version v1.0
 * 
 */
public class LoginForm extends ActionForm {
	
	private static final long serialVersionUID = -1162759977230749682L;

	private String username;
	
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

 四、编写LoginAction.java类

package com.raky.train.login;

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;

/**
 * struts1.x LoginAction
 * 
 * @author raky
 * @version v1.0
 * 
 */
public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		ActionForward forward = null;
		LoginForm loginForm = (LoginForm) form;

		if (loginForm.getUsername().equals("admin")
				&& loginForm.getPassword().equals("admin")) {
			
			forward = mapping.findForward("success");
			request.setAttribute("loginForm", loginForm);
		} else {
			forward = mapping.findForward("failure");
		}
		return forward;
	}
}

 

五、配置strutsConfig.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!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 Bean Definitions ==================== -->
	<form-beans>
		<form-bean name="loginForm" type="com.raky.train.login.LoginForm" />
	</form-beans>
	<!-- =================== Action Definitions ==================== -->
	<action-mappings>
		<action path="/raky/train/login" name="loginForm" type="com.raky.train.login.LoginAction" validate="false">
			<forward name="success" path="/raky/train/login/success.jsp" />
			<forward name="failure" path="/raky/train/login/failure.jsp" />
		</action>
	</action-mappings>

</struts-config>

 

 六、 编写登录页面(login.jsp) 

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - start - Login page</title>
</head>
<body>
<h2>raky - struts1.2 - start</h2>
<br/>
<html:form action="/raky/train/login" method="post">
<table>
	<tr>
		<td>用     户:</td>
		<td><html:text property="username"  maxlength="40" size="18" /></td>
	</tr>
	<tr>
		<td>密     码:</td>
		<td><html:password property="password" maxlength="40" size="18" /></td>		
	</tr>
	<tr>
		<td align="center" colspan="2">
			<html:submit property="submitValue" >确定</html:submit>
			<html:reset property="resetValue" >重置</html:reset>
		</td>
	</tr>
</table>
</html:form>
</body>
</html>

 

七、编写登录成功页面(success.jsp)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - start - Login success</title>
</head>
<body>
<table>
	<tr>
		<td>用     户:</td>
		<td>${loginForm.username}</td>
	</tr>
	<tr>
		<td>密     码:</td>
		<td>${loginForm.password}</td>		
	</tr>
</table>
<a href="javascript:history.back(-1)">返回</a>
</body>
</html>

 

八、编写登录失败页面(failure.jsp)

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - start - Login failure</title>
</head>
<body>
用户名或密码错误啦。。。 <a href="javascript:history.back(-1)">返回</a>
</body>
</html>

 

总结:适合自学者、初学者、无废话编程。

 

你可能感兴趣的:(java,struts,raky)