问题描述
- struts2会话域的问题,页面出来了,值出不来
-
package sjxy.struts2.servletapi; import java.util.Map; import org.apache.struts2.ServletActionContext; import sjxy.struts2.servletapi.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class ServletApiAction extends ActionSupport { public String test(){ ActionContext context=ServletActionContext.getContext(); Map request=(Map) context.get("request"); System.out.println("eeeeeee"); request.put("user", "张三");//在请求域中防止一个值: Map<String,Object> session=context.getSession();//获取回话域对象 User user=new User(); user.setName("李四"); user.setSex("男"); session.put("user", user); //获取上下文对象application Map<String,Object> application=context.getApplication(); Object value=application.get("count"); if(value==null){//说明是第一次被访问 application.put("count", new Integer(1)); }else{ Integer number=(Integer)value; number++; application.put("count", number); } return "success"; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="servletapi" namespace="/api" extends="struts-default"> <action name="user" class="sjxy.struts2.servletapi.ServletApiAction" > <result name="success">/WEB-INF/servlet/test.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <center> <h1>用户名称 :${requestScope.user }</h1><br><Hr> <h2>用户姓名:${sessionScope.user.name },性别:${sessionScope.user.sex }</h2><br> 当前页面被访问: <font color="red" size="30">[${applicationScope.count }]</font>次了! </center> </body> </html>
这是代码图,够不够?还要什么?
解决方案
时间: 2024-09-28 08:58:29