Struts2框架之Action获取请求参数的四种方式

本文介绍了Struts2框架中Action获取请求参数的四种方法:通过request、Action的属性set方法、属性set模型注入及模型驱动封装,并推荐了模型驱动封装方式,详细解析了每种方式的实现步骤和应用场景。
摘要由CSDN通过智能技术生成

方式一:通过request获取

①、LoginAction.java文件
package cn.hestyle.web.action;

import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class LoginAction {
   
    private String username;
    private String password;

    public String login(){
   
        //通过ServletActionContext获取request
        HttpServletRequest request = ServletActionContext.getRequest();
        //通过request获取参数
        username = request.getParameter("username");
        password = request.getParameter("password");
        System.out.println("username=" + username + "\tpassword=" + password);
        return "success";
    }
}

②、对应的struts.xml配置文件
<?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>
    <package name="user" extends="struts-default">
        <!-- 申明login动作,并使用LoginAction类中的login方法进行处理 -->
        <action name="login" class="cn.hestyle.web.action.LoginAction" method="login"></action>
    </package>
</struts>
③、前端index.jsp界面
<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2019/10/20
  Time: 2:35 下午
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/login.action" method="post">
        <table border="1px">
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值