Struts2学习笔记(7)-访问Web元素

前端技术 2023/09/02 Java

常用的Web元素有:request、session、application等,而我们一般使用session较多,Struts2如何访问web元素呢?这个是非常重要的内容,因为它能完成程序后台和用户的数据交互,下面以注册为例演示其过程:

1、index.jsp文件

<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" 
  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\" \"http://www.w3.org/TR/html4/loose.dtd\"> 
<html> 
<head> 
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> 
<base href=\"<%=basePath %>\"/> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>演示</h1> 
<form action=\"user/user02!register\" method=\"post\"> 
姓名:<input type=\"text\" name=\"user.name\"></input> 
<br/> 
密码:<input type=\"text\" name=\"user.password\"></input> 
<br/> 
<input type=\"submit\" value=\"注册\"/> 
</form> 
</body> 
</html> 

功能很简单--即用户输入用户名和密码,然后后台可以获得,然后注册成功后显示给用户

2、struts.xml 配置

<?xml version=\"1.0\" encoding=\"UTF-8\" ?> 
<!DOCTYPE struts PUBLIC 
  \"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN\" 
  \"http://struts.apache.org/dtds/struts-2.0.dtd\"> 
 
<struts> 
  <constant name=\"struts.devMode\" value=\"true\" /> 
  <package name=\"front\" namespace=\"/user\" extends=\"struts-default\"> 
 
    <action name=\"user*\" class=\"com.myservice.web.UserAction{1}\"> 
      <result>/success.jsp</result> 
      <result name=\"error\">/error.jsp</result> 
    </action> 
  </package> 
</struts> 

可以有两种方式完成这个功能

3、第一种(UserAction01)

package com.myservice.web; 
 
import java.util.Map; 
 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class UserAction01 extends ActionSupport { 
  /** 
   * 
   */ 
  private static final long serialVersionUID = 1L; 
  private User user; 
  private Map request; 
  private Map session; 
  private Map application; 
  public UserAction01(){ 
    request = (Map)ActionContext.getContext().get(\"request\"); 
    session = ActionContext.getContext().getSession(); 
    application = ActionContext.getContext().getApplication(); 
  } 
    public String register(){ 
    request.put(\"name\", user.getName()); 
    request.put(\"password\", user.getPassword()); 
    return SUCCESS; 
  } 
  public User getUser() { 
    return user; 
  } 
  public void setUser(User user) { 
    this.user = user; 
  } 
   
} 

这个方式是用ActionContext.getContext()方法获得context,然后得到request和session以及application

4、另外一种方式(UserAction02)非常常见,也是非常著名的方式-----Ioc(控制反转)和DI(依赖注入),它需要实现3个接口如下:

package com.myservice.web; 
 
import java.util.Map; 
 
import org.apache.struts2.interceptor.ApplicationAware; 
import org.apache.struts2.interceptor.RequestAware; 
import org.apache.struts2.interceptor.SessionAware; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
public class UserAction02 extends ActionSupport implements RequestAware, SessionAware,ApplicationAware{ 
  private Map<String, Object> request; 
  private Map<String, Object> session; 
  private Map<String, Object> application; 
  private User user; 
   
   
  public User getUser() { 
    return user; 
  } 
 
  public void setUser(User user) { 
    this.user = user; 
  } 
 
  public String register(){ 
    request.put(\"name\", user.getName()); 
    request.put(\"password\", user.getPassword()); 
    return SUCCESS; 
  } 
   
  @Override 
  public void setApplication(Map<String, Object> application) { 
    // TODO Auto-generated method stub 
    this.application = application; 
  } 
 
  @Override 
  public void setSession(Map<String, Object> session) { 
    // TODO Auto-generated method stub 
    this.session = session; 
  } 
 
  @Override 
  public void setRequest(Map<String, Object> request) { 
    // TODO Auto-generated method stub 
    this.request = request; 
  } 
 
}

这样就实现了一个功能--将user的名称和密码都放入request中,在使用时我们只需取出即可

5、success.jsp将request中内容取出并显示

<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" 
  pageEncoding=\"UTF-8\"%> 
<%@taglib uri=\"/struts-tags\" prefix=\"s\" %> 
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> 
<html> 
<head> 
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> 
<title>Insert title here</title> 
</head> 
<body> 
<h3>成功注册</h3> 
<s:property value=\"#request.name\"/>注册成功,密码为:<s:property value=\"#request.password\"/> 
</body> 
</html>

其结果显示为:

以上就是Struts2中访问Web元素的全部内容,希望能给大家一个参考,也希望大家多多支持phpstudy。

本文地址:https://www.stayed.cn/item/5734

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。