方式一:通过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(){
HttpServletRequest request = ServletActionContext.getRequest();
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