欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 18:00:26
常用的web元素有:request、session、application等,而我们一般使用session较多,struts2如何访问web元素呢?这个是非常重要的内容,因...

常用的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学习笔记(7)-访问Web元素

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

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