struts1.x心得2--第一个struts案例分析

   最重要的文件:struts-config.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-beans name="loginFrom" type=""></form-beans>
  <action-mappings>
    <action path="/login" name="loginFrom" type="">
      <forward name="loginSuccess" path="/loginSuccess.jsp"></forward>
      <forward name="loginFailure" path="/loginFailure.jsp"></forward>
    </action>
  </action-mappings>
</struts-config>
  Web.xml配置文件
<?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">
 
 <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>
   <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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'login.jsp' starting page</title>
    
  </head>
  
  <body>
  <!-- 这里叫login.do是因为web.xml配置中servlet对所有.do文件起作用,struts-config.xml中action的名字叫login -->
    <form action="<%=request.getContextPath()%>/login.do" method="post">
    username : <input type="text" name="username"><br><br>
    password : <input type="text" name="password"><br><br>
    <input type="submit" value="login">
    
    </form>
  </body>
</html>
   登录成功后的界面loginSuccess和失败后的界面loginFailure在这里不再赘述,就是输出成功或失败的两句话。
From文件,相当于javabean
package itcast.family.cn;

import org.apache.struts.action.ActionForm;

public class LoginFrom extends ActionForm {
	
	private String userName = null;
	private String password = null;
	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;
	}

}
Action文件,相当于servlet
package itcast.family.cn;

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;

public class LoginAction extends Action {

	//execute是现在struts2常用方法,以前老版本的叫做:perform
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//第一步强制转换为自己的类
		LoginFrom loginFrom = (LoginFrom)form; 
		//业务逻辑处理
		String returnURLKeyWord = null;
		if(loginFrom.getUserName().equals("yangkai")){
			returnURLKeyWord ="loginSuccess";
		}else{
			returnURLKeyWord ="loginFailure";
			
		}
		//跳转关键字
		return mapping.findForward(returnURLKeyWord);
	}
}

你可能感兴趣的:(java,java,java,java,java,java,Web,Web,语言,Struts1.X,Struts1.X,杨凯专属频道)