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>

<constant name="struts.devMode" value="true"/>

<package name="default" namespace="/user" extends="struts-default">
<action name="login" class="cn.bdqn.action.UserAction" method="login">
<result>/success.jsp</result>
</action>
</package>
</struts>


03.创建对应的UserAction



package cn.bdqn.action;


import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户登录的action
* jsp九大内置对象
*
* out
* page
* pageContext
* request
* response
* session
* application
* config
* exception
*/
public class UserAction extends ActionSupport{


/**
public String login(){
获取前台的值
01.耦合方式 不推荐使用
String name = ServletActionContext.getRequest().getParameter("name");
String password = ServletActionContext.getRequest().getParameter("password");
System.out.println(name);
System.out.println(password);
//放入作用域
ServletActionContext.getRequest().setAttribute("name",name);
ServletActionContext.getRequest().setAttribute("password",password);
return SUCCESS;
}*/

/**
* 登录的方法
02. 解耦两种方法
001. 使用ActionContext!
sturts2在底层把我们的request,session,application用Map集合保存起来了!
*/
private String name;
private String password;

public String login(){
Map<String, Object> request=(Map<String, Object>) ActionContext.getContext().get("request");
//让success.jsp获取数据
request.put("name",name);// 等同于setAttribute("name",name);
request.put("password",password);
return SUCCESS;
}



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.使用第二种解耦的方式 ,就是实现对应的Aware接口



import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 实现对应的Aware接口
*/
public class UserAction2 extends ActionSupport implements RequestAware{

private String name;
private String password;
private Map<String, Object> request; //并不用写 set和get

//登录的方法
public String login(){
request.put("name",name);
request.put("password",password);
return SUCCESS;
}

//重写方法 给请求request赋值
@Override
public void setRequest(Map<String, Object> request) {
this.request=request;

}


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;
}







}


05.success.jsp



===============el表达式获取数据=============<br/>
${name}
${password} <br/>
===============struts2标签获取数据=============<br/>
<s:property value="password"/> <%--值栈中获取的 --%>
<s:property value="#request.password"/> <%--栈的上下文中获取的 --%>
<s:property value="#attr.password"/>


 


作者:Rick__想太多先森