Action中的JAVA代码

   public String userView(){
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("value","jeffery");
return SUCCESS;
}
JSP页面
<%String value = (String)request.getAttribute("value"); 
  out.print(value);
%>
问题request.getAttribute("value") 取不到值,是空值。Action里面request.setAttribute("value","jeffery"); 已经SET了,用GET取值不到。我现在都是用的SESSION取值,假如用户量一多,我的服务器肯定会死的。

解决方案 »

  1.   

    你确定这样取到的是request对象?
    其实你的action也可以实现RequestAware接口,覆盖RequestAware的setRequest()方法,如下:
    public class XxxAction implements RequestAware{
      public Map request;
      
      public void setRequest(Map request){
         this.request = request;
      }
      
      public String userView()throws Exception{
          request.put("value","jeffery");
          return SUCCESS;
     }
    }
    这样看在jsp页面能不能取到。
      

  2.   

    貌似一般不这么用 你把你要传值的类型设为一个变量 在action里加上get set方法 然后再action方法里set一次 前台就可以用了。。
      

  3.   

    哪里取不到了??
    你别的地方有错吧??
    给你个例子,套你的reques不是好好的么??
    actionpackage com.lil.test_ch03_02.action;import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ModelDriven;
    import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;import com.lil.test_ch03_02.entity.User;public class LoginAction implements Action, ModelDriven<User> {
    private User user=new User();

    public User getModel() {
    return user;
    }

    public String execute() throws Exception {
    if("zhangsan".equals(user.getUsername()) && "123".equals(user.getPassword())) {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("value","jeffery");

        System.out.println(request.getAttribute("value"));
        
    return SUCCESS;
    }

    return ERROR;
    }
    }
    entitypackage com.lil.test_ch03_02.entity;import java.io.Serializable;public class User implements Serializable {
    private String username;
    private String password;

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username=username;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password=password;
    }
    }login.jsp<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'login.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>
        <form action="login.action">
         Username: <input type="text" name="username" />
         <br />
         Password: <input type="password" name="password" />
         <br />
         <input type="submit" value="Submit"/>
         <input type="reset" value="reset"/>
        </form>
      </body>
    </html>
    success.jsp<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'success.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>
        <s:property value="username" />, welcome!
        <br />
    <%
    String value = (String)request.getAttribute("value");
    out.print("value="+value);
    %>
      </body>
    </html>
    配置<?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>
    <package name="default" extends="struts-default">
    <action name="login" class="com.lil.test_ch03_02.action.LoginAction">
    <result>/success.jsp</result>
    <result name="error">/error.jsp</result>
    </action>
    </package>
    </struts>
    跑不出来你找我
      

  4.   

    struts2 在 在action中定义变量,同时加上对应的set 、get 方法,就可以在页面直接用,从页面往后台返回时也可以直接用
      

  5.   

    谢谢大家,来帮我的都给分数。
       问题我已经解决,让给大家一起分享。
    例如:sturts.xml      <action name="CoursesSecondMenuOrder" class="com.manager.action.CoursesSecondMenuOrder">
           <result  name="success" type="redirect">/basic/admin/CoursesSecondMenuView.action</result>
         </action>看到上面没有type="redirect"  我加了这一个属性就不能取request的值了。把这一项去掉,就OK了。
    还是要谢谢大家的帮助。有分大家一起分。哈哈
      

  6.   

    用struts2了为什么还用这种传值的方式呢??。。
      

  7.   

    struts2:
          继承 ActionSupport后,只要getUser()后      jsp页面:request.getAttrbute("user") 可以取到。