Struts第七天--使用request,application,session

来源:互联网 发布:工程进度编制软件 编辑:程序博客网 时间:2024/05/20 01:09

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.i18n.encoding" value="utf-8"/>
 <package name="person" namespace="/person" extends="struts-default">
  <action name="manage" class="com.isoftstone.study.HelloWorldAction" method="execute">   
   <result name="message">/WEB-INF/page/hello.jsp</result> 
  </action>  
 </package> 
</struts> 

package com.isoftstone.study;
import java.util.Arrays;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
public class HelloWorldAction { 
 public String execute() throws Exception{
  ActionContext ctx=ActionContext.getContext();
  ctx.getApplication().put("app","应用范围");
  ctx.getSession().put("ses", "session范围");
  ctx.put("req", "request范围哦"); 
  ctx.put("names", Arrays.asList("老方","老张","老黎"));
  return "message";
 }
 public String ras() throws Exception{
  HttpServletRequest request=ServletActionContext.getRequest();
  ServletContext servletContext=ServletActionContext.getServletContext();
  request.setAttribute("req", "请求范围内属性");
  request.getSession().setAttribute("ses", "回话范围内属性");
  servletContext.setAttribute("app", "回话范围内属性");
  //HttpServletResponse response=ServletActionContext.getResponse();
  return "message";
 }

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'hello.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    ${applicationScope.app }<br/>
    ${sessionScope.ses }<br/>
    ${requestScope.req }
    =========================================\\
    <c:forEach items="${names}" var="name">
     ${name }<br/>
    </c:forEach>
  </body>
</html>