Struts2學(xué)習(xí)筆記(7)-訪問Web元素
常用的Web元素有:request、session、application等,而我們一般使用session較多,Struts2如何訪問web元素呢?這個(gè)是非常重要的內(nèi)容,因?yàn)樗芡瓿沙绦蚝笈_(tái)和用戶的數(shù)據(jù)交互,下面以注冊為例演示其過程:
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>
功能很簡單--即用戶輸入用戶名和密碼,然后后臺(tái)可以獲得,然后注冊成功后顯示給用戶
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>
可以有兩種方式完成這個(gè)功能
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;
}
}
這個(gè)方式是用ActionContext.getContext()方法獲得context,然后得到request和session以及application
4、另外一種方式(UserAction02)非常常見,也是非常著名的方式-----Ioc(控制反轉(zhuǎn))和DI(依賴注入),它需要實(shí)現(xiàn)3個(gè)接口如下:
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;
}
}
這樣就實(shí)現(xiàn)了一個(gè)功能--將user的名稱和密碼都放入request中,在使用時(shí)我們只需取出即可
5、success.jsp將request中內(nèi)容取出并顯示
<%@ 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>
其結(jié)果顯示為:


以上就是Struts2中訪問Web元素的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐
本文主要介紹了springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Java TimedCache 帶時(shí)間緩存工具類詳解使用
工具類是包含集合框架、遺留的 collection 類、事件模型、日期和時(shí)間設(shè)施、國際化和各種實(shí)用工具類(字符串標(biāo)記生成器、隨機(jī)數(shù)生成器和位數(shù)組、日期Date類、堆棧Stack類、向量Vector類等)。集合類、時(shí)間處理模式、日期工具等各類常用工具包,本文將介紹帶時(shí)間緩存工具類2021-10-10
java 多線程實(shí)現(xiàn)在線咨詢(udp)
這篇文章主要介紹了java 多線程實(shí)現(xiàn)在線咨詢(udp)的示例,幫助大家更好的理解和學(xué)習(xí)Java 網(wǎng)絡(luò)編程的相關(guān)內(nèi)容,感興趣的朋友可以了解下2020-11-11
Spring Cloud Nacos 和 Eureka區(qū)別解析
Spring Cloud Nacos 和 Spring Cloud Eureka 都是 Spring Cloud 微服務(wù)框架中的服務(wù)注冊和發(fā)現(xiàn)組件,用于幫助開發(fā)者輕松地構(gòu)建和管理微服務(wù)應(yīng)用,這篇文章主要介紹了Spring Cloud Nacos 和 Eureka區(qū)別,需要的朋友可以參考下2023-08-08
Spring?Boot中WebMvcConfig配置詳解及示例代碼
WebMvcConfig是一個(gè)配置類,它繼承了WebMvcConfigurationSupport,允許我們對SpringMVC進(jìn)行更細(xì)粒度的控制,這篇文章主要給大家介紹了關(guān)于Spring?Boot中WebMvcConfig配置詳解及示例的相關(guān)資料,需要的朋友可以參考下2024-03-03

