Struts2(三)参数传递

复制代码
package cn.bdqn.bean;

/**
 * 
 *用户的实体类
 */
public class User {
 
    private  String  name;
    private  String  password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public User(String name, String password) {
        super();
        this.name = name;
        this.password = password;
    }
    public User() {
        super();
    }
    
    
}
复制代码

 

 

一:前台传递单个属性

01.创建登录界面

复制代码
<%@ page language="java" import="java.util.*" 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 'index.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>
   <form action="user/login" method="post">
       <table>
            <tr>
               <td>用户名:</td>
               <td><input  type="text" name="name"/></td>
            </tr>
            <tr>
               <td>密码:</td>
               <td><input  type="password" name="password"/></td>
            </tr>
            <tr>
               <td></td>
               <td><input  type="submit" value="登录"/></td>
            </tr>
       </table>
   </form>
  </body>
</html>
复制代码

 

02.在struts.xml中配置

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <package name="default"  namespace="/user" extends="struts-default">
        <!--用户登录的action  -->
        <action name="login" class="cn.bdqn.action.LoginAction">
           <!--如果返回值是success,则可以省略name属性  -->
            <result name="success">/loginSuccess.jsp</result>
        </action>
    </package>
</struts>
复制代码

03.创建对应的Action

复制代码
package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 *  用户登录的action
 */
public class LoginAction extends ActionSupport {

    /**
     * 01.定义和表单中name属性值一致的成员变量,并且具有get和set
     */
    private  String  name;
    private  String  password;
    
    
    @Override
    public String execute() throws Exception {
        System.out.println("用户名:"+name);
        System.out.println("密码:"+password);
        return "success"; //"success"    SUCCESS  super.execute()
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    

}
复制代码

 

04.创建登录成功 页面

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.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>
  =============== 通过el表达式获取后台的数据     默认的作用是 request===============<br/>
  ${requestScope.name} <br/>
  ${password}<br/>
  
   =============== 通过struts2的标签获取===============<br/>
   <s:property value="name"/><br/>
   <s:property value="password"/><br/>
   
   <s:debug/>
    
  </body>
</html>
复制代码

 

二:前台传递对象

 修改login.jsp页面

复制代码
<%@ page language="java" import="java.util.*" 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 'index.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>
   <form action="user/login" method="post">
       <table>
            <tr>
               <td>用户名:</td>
               <td><input  type="text" name="user.name"/></td>
            </tr>
            <tr>
               <td>密码:</td>
               <td><input  type="password" name="user.password"/></td>
            </tr>
            <tr>
               <td></td>
               <td><input  type="submit" value="登录"/></td>
            </tr>
       </table>
   </form>
  </body>
</html>
复制代码

02.struts.xml文件中的代码不需要改变

03.修改LoginAction中的代码

复制代码
package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 *  用户登录的action
 */
public class LoginAction extends ActionSupport {

    /**
     * 02.直接定义前台表单中的对象!并且具有get和set!  这是我们常使用的方式
     */
    private User user;

    
    @Override
    public String execute() throws Exception {
        System.out.println("用户名:"+user.getName());
        System.out.println("密码:"+user.getPassword());
        return "success"; //"success"    SUCCESS  super.execute()
    }

    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }


    
    
    

}
复制代码

 

04.修改loginSuccess.jsp页面

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.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>
  =============== 通过el表达式获取后台的数据     默认的作用是 request===============<br/>
  ${requestScope.user.name} <br/>
  ${user.password}<br/>
  
   =============== 通过struts2的标签获取===============<br/>
   <s:property value="user.name"/><br/>
   <s:property value="user.password"/><br/>
   
   <s:debug/>
    
  </body>
</html>
复制代码

 

三:实现ModelDriven接口

其他代码和单个属性传递 一致!

只需要更改LoginAction中的代码即可!

复制代码
package cn.bdqn.action;

import cn.bdqn.bean.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 *  用户登录的action
 */
public class LoginAction extends ActionSupport implements ModelDriven {

    /**
     * 03.实现ModelDriven   前台的name属性值 必须要和user对象中的属性名一致!不需要user.
     */
    private User user=new User();

    
    @Override
    public String execute() throws Exception {
        System.out.println("用户名:"+user.getName());
        System.out.println("密码:"+user.getPassword());
        return "success"; //"success"    SUCCESS  super.execute()
    }

    @Override
    public Object getModel() {
        return user;
    }


    
    
    

}
复制代码

 

Struts.xml文件中一些常量的设置以及验证默认转发

复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--  struts2的默认扩展名 
struts.action.extension=action,,
01.以action结尾   02.什么都不写
<constant name="struts.action.extension" value="do,t10"/>

### - struts.i18n.reload = true
### - struts.configuration.xml.reload = true
 struts.devMode = false //手动重启
 
 01.开发模式: 修改代码多    struts.devMode =true   自动重启
 02.生产模式: 项目发布       struts.devMode = false 手动重启
 -->
 <constant name="struts.devMode" value="true"/><!--  设置开发模式  -->
    <package name="default"  namespace="/user" extends="struts-default">
        <!--用户登录的action  -->
        <action  name="login" class="cn.bdqn.action.LoginAction">
           <!--如果返回值是success,则可以省略name属性  
             type:的默认值 是转发!
                                                        怎么验证转发?
                 看浏览器中的url是不是action的属性值!是!就是转发!
                 如果是 loginSuccess.jsp说明是重定向!                                                
             type="redirect":重定向!
           -->
            <result name="success">/loginSuccess.jsp</result>
        </action>
    </package>
</struts>
复制代码

 

posted @ 2018-04-25 11:40  ★半支烟`☆  阅读(204)  评论(0编辑  收藏  举报
··