您的位置:首页 > 编程语言 > Java开发

Struts2标签库(3):利用<s:property.../>标签访问Struts2的命名对象

2013-06-25 13:03 399 查看


start.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="sample" method="post">
username:<input type="text" name="username"><br>
<input type="submit" value="confirm">
</form>
</body>
</html>
struts.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
<package name="demo" extends="struts-default">
<action name="sample" class="action.SampleAction">
<result name="success">/showInfo.jsp</result>
</action>
</package>
</struts>
SampleAction.java :

public class SampleAction {

private String username;
private String msg;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}

public String execute(){
System.out.println("username:"+username);
msg="Action Value Message";

Map<String,Object> request=
(Map<String,Object>)ActionContext.getContext().get("request");
request.put("msg","Request Value Message");

Map<String,Object> session=ActionContext.getContext().getSession();
session.put("msg","Session Value Message");

Map<String,Object> application=ActionContext.getContext().getApplication();
application.put("msg","Application Value Message");

return "success";
}
}
showInfo.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<b>action message</b>:<s:property value="msg"/><br>
<b>request message</b>:<s:property value="#request.msg"/><br>
<b>session message</b>:<s:property value="#session.msg"/><br>
<b>application message</b>:<s:property value="#application.msg"/><br>
<b>attr message</b>:<s:property value="#attr.msg"/><br>
<b>action parameters</b>:<s:property value="username"/><br>
<b>context parameters</b>:<s:property value="#parameters.username"/><br>
<s:debug></s:debug>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: