Struts逐步学习(1)-环境搭建

1. 首先要下载struts的jar包,地址 http://struts.apache.org/
2. 解压Strusts包后,将lib文件夹里的jar包都加入到项目中WEB-INF下的lib文件夹下
3. 在解压后的webapps文件夹中解压struts-blank.war,将其中的struts-config.xml拷贝到项目中( 避免手写出错,复制粘贴,做一些配置)
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.tgb.struts.LoginActionForm"></form-bean>
	</form-beans>
	
	<action-mappings>
		<action path="/login"
				type="com.tgb.struts.LoginAction"
				name="loginForm"
				scope="request"
				>
			<forward name="success" path="/login_success.jsp"></forward>
			<forward name="error" path="/login_error.jsp"></forward>
	
				</action>
				
	</action-mappings>

</struts-config>

4. 配置web.xml部署ActionServlet。这个格式是相对固定的,按要求配置路径和类信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.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/struts-config.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>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


</web-app>

     5、新建一个LoginAction类,继承 Action:
package com.tgb.struts;

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;

/**
 * 
 * 负责取得表单数据,调用业务逻辑,返回转向信息
 * @author Jesse
 *
 */
public class LoginAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		LoginActionForm laf=(LoginActionForm)form;
		String username=laf.getUsername();
		String password=laf.getPassword();
		
		if("admin".equals(username)&& "admin".equals(password)){
			//登陆成功
			 return mapping.findForward("success");
			
		}else{
			//登录失败
			 return mapping.findForward("error");
		}	
	}
}

      6、新建一个LoginActionForm类,继承ActionForm:
package com.tgb.struts;

import org.apache.struts.action.ActionForm;

/**
 * 登陆ActionForm,负责表单数收集数据
 * @author Jesse
 *
 */
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {
	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;
	}
}
      7、login.Jsp页面:
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
	<form action="login.do" method="post" >
	用户:<input type="text" name="username"><br>
	密码:<input type="password" name="password"><br>
	<input type="submit" value="登陆">
	
	</form>
</body>
</html>

        8、success.jsp ,error.jsp页面省略,只是一个显示界面。

   至此,一个简单的Struts环境登陆实例就搭建起来啦。

你可能感兴趣的:(struts)