快乐编程-知识分享-Struts1.x学习-1.3开始-002

目标:运行第二个struts1.x程序(struts1.3)

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

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

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

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

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

工程截图:


快乐编程-知识分享-Struts1.x学习-1.3开始-002

 一、导入相关包,具体如下截图(struts1.3 lib)
快乐编程-知识分享-Struts1.x学习-1.3开始-002

二、配置工程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.3-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/config/struts/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>index.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.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

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

	public ActionForward login(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.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.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" parameter="method" scope="request"
			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>

 

六、 编写主页面(index.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>index page</title>
</head>
<body>
<jsp:forward page="/raky/train/login/login.jsp"  />
</body>
</html>

 

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

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<!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.3 - start - Login page</title>
</head>
<body>
<h2>raky - struts1.3 - start</h2>
<html-el:form action="/raky/train/login" method="post" onsubmit="return check();">
<html-el:hidden property="method" value="login" />
<table>
	<tr>
		<td>用     户:</td>
		<td><html-el:text styleId="username" property="username"  maxlength="40" size="18" /></td>
	</tr>
	<tr>
		<td>密     码:</td>
		<td><html-el:password styleId="password" property="password"  maxlength="40" size="18" /></td>		
	</tr>
	<tr>
		<td align="center" colspan="2">
			<html-el:submit property="submitValue" >确定</html-el:submit>
			<html-el:reset property="resetValue" >重置</html-el:reset>
		</td>	
	</tr>
</table>
</html-el:form>
<script type="text/javascript" language="javascript">
function check(){
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	if(username == ""){
		alert("用户名不为空!");
		document.forms[0].username.focus();
		return false;
	}
    if(password == ""){   
        alert("密码不能为空!");   
        document.forms[0].password.focus();  
        return false; 
    }	
	return true;
}
</script>
</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.3 - start - Login success</title>
</head>
<body>
用     户:${loginForm.username}   密     码:${loginForm.password} <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.3 - start - Login failure</title>
</head>
<body>
用户名或密码错误啦。。。 <a href="javascript:history.back(-1)">返回</a>
</body>
</html>

 

总结:本用例程序是struts1.3,注意与之前struts1.2的区别,重点在使用标签库不同,另页面加入了简单的js验证,控制器用了DispatchAction。

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