struts-config.xml配置文件的scope

一 scope说明
指定ActionForm Bean的存在范围,可选取request和session,默认是session。
 
二 通过request取数据
LoginAction
package com.cakin.actions;
//这是一个action(表示小队长,需要继承Action)
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 com.cakin.forms.UserForm;
public class LoginAction extends Action {
    //我们需要重新编写一个方法:execute会被自动调用,有点类似servlet的service方法。
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            //把form转成对应的UserForm对象
        UserForm userForm=(UserForm)form;
        //简单验证
        if("123".equals(userForm.getPassword())){
            //把名字存放到request对象域
            //request.setAttribute("username", userForm.getUsername());
            return mapping.findForward("ok");
        }
        else{
            return mapping.findForward("err");
        }
    }
}
 
wel.jsp
<%@  page  language = "java"  import = "java.util.*"  import = "com.cakin.forms.*"  pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE  HTML  PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
   < head >
     < base  href = " <%= basePath %> " >
   
     < title > My JSP 'wel.jsp' starting page </ title >
   
         < meta  http-equiv = "pragma"  content = "no-cache" >
         < meta  http-equiv = "cache-control"  content = "no-cache" >
         < meta  http-equiv = "expires"  content = "0" >    
         < meta  http-equiv = "keywords"  content = "keyword1,keyword2,keyword3" >
         < meta  http-equiv = "description"  content = "This is my page" >
         <!--
        <link  rel =" stylesheet " type="text/ css "  href ="styles.css">
        -->
 
   </ head >
 
   < body >
    welcome  <%= ((UserForm)request.getAttribute( "userForm" )).getUsername()  %>  < br >
     < a  href = "/strutslogin/" > 返回重新登录 </ a >
   </ body >
</ html >
 
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是表单名字,可以随意写,但我们建议取名规范,表单为类名小写 -->
         <!-- type用于指定表单类全路径 -->
         < form-bean  name = "userForm"  type = "com.cakin.forms.UserForm" ></ form-bean >
   </ form-beans >
   < action-mappings >
     <!-- 配置具体的一个action, path:表示将来访问action的 url http://localhost:8080/web/path -->
         <!--  name用于关联某个表单-->
         <!--  type用于指定该action类全路径-->
         <!--  request表示该action对应的表单对象的生命周期request=request.setAttribute("userForm",userForm)-->
         <!--  session表示该action对应的表单对象的生命周期session=request.getSession.setAttribute("userForm",userForm)-->
         < action  path = "/login"  name = "userForm"  scope = "request"  type = "com.cakin.actions.LoginAction" >
         <!--  这里配置跳转关系-->
             <!--name表示结果名称 path:转发到哪个页面  -->
                 < forward  name = "ok"  path = "/WEB-INF/wel.jsp" ></ forward >
                 < forward  name = "err"  path = "/WEB-INF/err.jsp" ></ forward >
         </ action >
 
        
   </ action-mappings >
</ struts-config >
 
三  测试结果


 
 
四 通过session 取数据
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是表单名字,可以随意写,但我们建议取名规范,表单为类名小写 -->
         <!-- type用于指定表单类全路径 -->
         < form-bean  name = "userForm"  type = "com.cakin.forms.UserForm" ></ form-bean >
   </ form-beans >
   < action-mappings >
     <!-- 配置具体的一个action, path:表示将来访问action的 url http://localhost:8080/web/path -->
         <!--  name用于关联某个表单-->
         <!--  type用于指定该action类全路径-->
         <!--  request表示该action对应的表单对象的生命周期request=request.setAttribute("userForm",userForm)-->
         <!--  session表示该action对应的表单对象的生命周期session=request.getSession.setAttribute("userForm",userForm)-->
         < action  path = "/login"  name = "userForm"    type = "com.cakin.actions.LoginAction" >
         <!--  这里配置跳转关系-->
             <!--name表示结果名称 path:转发到哪个页面  -->
                 < forward  name = "ok"  path = "/WEB-INF/wel.jsp" ></ forward >
                 < forward  name = "err"  path = "/WEB-INF/err.jsp" ></ forward >
         </ action >
 
        
   </ action-mappings >
</ struts-config >
wel.jsp
<%@  page  language = "java"  import = "java.util.*"  import = "com.cakin.forms.*"  pageEncoding = "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<! DOCTYPE  HTML  PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
   < head >
     < base  href = " <%= basePath %> " >
   
     < title > My JSP 'wel.jsp' starting page </ title >
   
         < meta  http-equiv = "pragma"  content = "no-cache" >
         < meta  http-equiv = "cache-control"  content = "no-cache" >
         < meta  http-equiv = "expires"  content = "0" >    
         < meta  http-equiv = "keywords"  content = "keyword1,keyword2,keyword3" >
         < meta  http-equiv = "description"  content = "This is my page" >
         <!--
        <link  rel =" stylesheet " type="text/ css "  href ="styles.css">
        -->
 
   </ head >
 
   < body >
    welcome  <%= ((UserForm)session.getAttribute( "userForm" )).getUsername()  %>  < br >
     < a  href = "/strutslogin/" > 返回重新登录 </ a >
   </ body >
</ html >
 
五 测试结果


 

猜你喜欢

转载自cakin24.iteye.com/blog/2397615
今日推荐