程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts2進修筆記(7)-拜訪Web元素

Struts2進修筆記(7)-拜訪Web元素

編輯:關於JAVA

Struts2進修筆記(7)-拜訪Web元素。本站提示廣大學習愛好者:(Struts2進修筆記(7)-拜訪Web元素)文章只能為提供參考,不一定能成為您想要的結果。以下是Struts2進修筆記(7)-拜訪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中拜訪Web元素的全體內容,願望能給年夜家一個參考,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved