JavaWeb_(Struts2框架)参数传递之接收参数与传递参数

 

 

一、接收参数

  第一种方法

  【常用】1、继承implements ModelDriven

public class UserAction extends ActionSupport implements ModelDriven{

 

      2、创建一个对象,不要加get()和set()方法

public User user = new User();

 

      3、实现getModel()方法

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }

 

JavaWeb_(Struts2框架)参数传递之接收参数与传递参数_第1张图片

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven{

    public User user = new User();
    
    
    
    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(user.getUsername());
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }
    
    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

    //kill方法
    public String kill() throws Exception {
        
        System.err.println("kill()方法");
        
        return null;
    }
    
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
UserAction.java

 

 

  第二种方法

  1、添加属性驱动

//添加属性驱动
    public String username;
    public 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;
    }

 

JavaWeb_(Struts2框架)参数传递之接收参数与传递参数_第2张图片

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport{

    //public User user = new User();
    
    //添加属性驱动
    public String username;
    public 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;
    }

    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(username+":"+ password);
        
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }
    
    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

    //kill方法
    public String kill() throws Exception {
        
        System.err.println("kill()方法");
        
        return null;
    }

}
UserAction.java

 

 

  第三种方法

  在login.jsp中修改登陆和注册标签框

                <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="user.username" type="text" placeholder="您的用户名和登录名" />
                label>

 

                <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="user.password" placeholder="建议至少使用两种字符组合" />
                label>

 

  UserAction.java中直接通过user.getUsername()和user.setUsername()直接得到前台获取到的值

public User user;

    public User getUser() {
        return user;
    }

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

    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(user.getUsername()+":"+ user.getPassword());
        
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }

 

JavaWeb_(Struts2框架)参数传递之接收参数与传递参数_第3张图片

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="css/head.css" />
<link rel="stylesheet" type="text/css" href="css/login.css" />
head>

<body>
    <div class="dvhead">
        <div class="dvlogo">
            <a href="index.html">你问我答a>
        div>
        <div class="dvsearch">10秒钟注册账号,找到你的同学div>
        <div class="dvreg">
            已有账号,立即 <a href="login.html">登录a>
        div>
    div>
    <section class="sec">
        <form action="${pageContext.request.contextPath }/LoginAction_login" method="post">
            <div class="register-box">
                <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="user.username" type="text" placeholder="您的用户名和登录名" />
                label>
                <div class="tips">div>
            div>
            <div class="register-box">
                <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="user.password" placeholder="建议至少使用两种字符组合" />
                label>
                <div class="tips">div>
            div>
            <div class="arguement">
                <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》a> <a href="register.html">没有账号,立即注册a>
                <div class="tips" style="color: red">${error}div>
            div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">立 即 登录button>
            div>
        form>
    section>
    <script src="js/index.js" type="text/javascript" charset="utf-8">script>
body>
login.jsp

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport{

    public User user;

    public User getUser() {
        return user;
    }

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

    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(user.getUsername()+":"+ user.getPassword());
        
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }
    
    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

    //kill方法
    public String kill() throws Exception {
        
        System.err.println("kill()方法");
        
        return null;
    }

}
UserAction.java

 

 

二、传递参数

  在ImplAction.java中通过login()方法添加到session域和application域中

    public String login() throws Exception
    {
        //得到原生的request域
        //ServletActionContext.getRequest().setAttribute("username", "123");
        //ServletActionContext.getRequest().setAttribute("password", "123");
        
        ActionContext.getContext().put("username", "123");
        ActionContext.getContext().put("password", "123");
        
        //获得session
        Map session = ActionContext.getContext().getSession();
        session.put("mysession", "这是session域");
        //获得application
        Map application = ActionContext.getContext().getApplication();
        application.put("myapplication", "这是application域");
        
        //获得原生的request
        HttpServletRequest request = ServletActionContext.getRequest();
        
        //获得原生的response
        HttpServletResponse response = ServletActionContext.getResponse();
        
        return "toLogin";
    }

 

  struts.xml中动态方法配置了action,将web层ImplAction中的login()转发到com.Gary.web.UserAction中的login()方法

<action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
            
            <result name="success" type="redirect">/index.htmlresult>
            
            <result name="error">/login.jspresult>
        action>


<action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
            
            <result name="defaultAction" type="chain">LoginActionDefaultresult>
            
            <result name="toLogin" type="redirectAction">
                <param name="actionName">LoginAction_loginparam>
                
                <param name="username">${username}param>
                <param name="password">${password}param>
            result>
        action>

 

  在UserAction.java中输出放置在session域和application域中的内容

    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(user.getUsername()+":"+user.getPassword());
        
        System.out.println("session域..."+ActionContext.getContext().getSession().get("mysession"));
        System.out.println("application域..."+ActionContext.getContext().getApplication().get("myapplication"));
        
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }

 

JavaWeb_(Struts2框架)参数传递之接收参数与传递参数_第4张图片

 

package com.Gary.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class ImplAction implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("这是实现了Action接口的action");
        return "defaultAction";
    }

    
    public String login() throws Exception
    {
        //得到原生的request域
        //ServletActionContext.getRequest().setAttribute("username", "123");
        //ServletActionContext.getRequest().setAttribute("password", "123");
        
        ActionContext.getContext().put("username", "123");
        ActionContext.getContext().put("password", "123");
        
        //获得session
        Map session = ActionContext.getContext().getSession();
        session.put("mysession", "这是session域");
        //获得application
        Map application = ActionContext.getContext().getApplication();
        application.put("myapplication", "这是application域");
        
        //获得原生的request
        HttpServletRequest request = ServletActionContext.getRequest();
        
        //获得原生的response
        HttpServletResponse response = ServletActionContext.getResponse();
        
        return "toLogin";
    }
    
    
}
ImplAction.java

 

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

    <constant name="struts.devMode" value="true">constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true">constant>

    
    <package name="MyPackage" namespace="/" extends="struts-default">
        
        <global-allowed-methods>regex:.*global-allowed-methods>
    
        
            
        <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
            
            <result name="success" type="redirect">/index.htmlresult>
            
            <result name="error">/login.jspresult>
        action>


        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        
        action>
        
        <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
            
            <result name="defaultAction" type="chain">LoginActionDefaultresult>
            
            <result name="toLogin" type="redirectAction">
                <param name="actionName">LoginAction_loginparam>
                
                <param name="username">${username}param>
                <param name="password">${password}param>
            result>
        action>
    package>
struts>
struts.xml

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven{

    public User user = new User();
    
    
    
    public String login() throws Exception {
        
        System.out.println("login()方法");
        System.out.println(user.getUsername()+":"+user.getPassword());
        
        System.out.println("session域..."+ActionContext.getContext().getSession().get("mysession"));
        System.out.println("application域..."+ActionContext.getContext().getApplication().get("myapplication"));
        
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }
    
    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

    //kill方法
    public String kill() throws Exception {
        
        System.err.println("kill()方法");
        
        return null;
    }
    
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
UserAction.java

 

转载于:https://www.cnblogs.com/1138720556Gary/p/11573412.html

你可能感兴趣的:(JavaWeb_(Struts2框架)参数传递之接收参数与传递参数)