在没用struts1.2 的时候可以用request.setAttribute() 给servlet 传值,servlet用request.getAttribute() 接收该值。在运用了struts1.2 后request.setAttribute() 给一个action ,就无法用request.getAttribute() 取得该值,请问在struts1.2中要达到此目的一般这样处理?假设我传递一个bean的类型过去,我试着在页面用一个隐藏空间来设置这个bean值,但是actionForm无法接受bean类型的数据,大家帮帮忙
解决方案 »
- linux下的编码问题?在线等
- 急!!两个关于服务器端构建的问题!
- 希望大家可以讨论一下java的内容管理系统和php的发展前景
- 如何在struts中根据actionform的值来设定jsp中的radio选中状态
- 高分求助,很奇怪但很简单的问题...
- 用java生成附加码(网站登陆的时候都需要用户添加附加码)
- 一个简单页面出错???晕
- 各位大虾,我在使用resin的数据库连接池时发生了错误,请大家帮我看看怎么回事。很急!!!
- 新手求教struts中的数据库连接
- 有关JSplitPane的用法,有点想不通,怎么回事?
- 有关于树形结构选中的问题求教
- jsp+javaBean 导入DWR框架,老报js错误(请高手解决下)
你在org.apache.struts.action.Action 的方法execute 设定
request.setAttribute, 在你所要提交的jsp页面上就可以用
request.getAttribute的.
public class UserForm extends ActionForm {
/*
* Generated fields
*/ /** password property */
private String password; /** username property */
private String username;****get set 就不贴了
}-----------------------------------------------------------------------------------------------
Action:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
String username = userForm.getUsername();
String password = userForm.getPassword();
String abc = (String) request.getAttribute("abc");
String bcd = (String) request.getAttribute("bcd");
ProBean proBean = (ProBean) request.getAttribute("proBean");
System.out.println(username);
System.out.println(password);
System.out.println(abc); //null
System.out.println(bcd); //null
//System.out.println(proBean.getName()); //这两句空指针了。取不到值
//System.out.println(proBean.getId());
return null;
}----------------------------------------------------------------------------------------------------------------
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="com.liang.struts.form.UserForm" /> </form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
name="userForm"
path="/user"
scope="request"
type="com.liang.struts.action.UserAction" /> </action-mappings> <message-resources parameter="com.liang.struts.ApplicationResources" />
</struts-config>-------------------------------------------------------------------------------------jsp:<body>
<html:form action="user.do" method="post" focus="login">
<table border="0">
<tr>
<td>Login:</td>
<td><html:text property="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><html:password property="password" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
<%
request.setAttribute("abc","abc");
request.setAttribute("bcd","bcd");
ProBean pro = new ProBean();
pro.setName("proname");
pro.setId("proid");
request.setAttribute("proBean",pro);
%>
</table>
</html:form>
</body>-------------------------------------------------------------------------setAttribute 的东西在action里都收不到哦。结果:
asdf
asdf
null
null
request.getRequestDispatcher("user.do").forward(request,response);
转发
但是这样页面都打不开了。直接转发,
然后控制台也没有打印东西我还试了一个servlet也不行,是不是jsp页面不能setAttribute 给 .class的啊?
2009-6-5 11:33:51 org.apache.commons.beanutils.PropertyUtilsBean invokeMethod
严重: Method invocation failed.
java.lang.IllegalArgumentException: argument type mismatch要setAttribute的话可以用session实现。具体不大清楚什么原因。网上查了说是页面到类的request属于不可见,已不属于同一个请求范围那位朋友能帮忙解释一下啊?
这位朋友的方法可行,在struts1.2中这样可以让页面收到。但是这个用脚本怎么写不知道怎么写。直接写里面,直接就转发了。
还有如果用request.getRequestDispatcher("servlet").forward(request,response); 提交给一个servlet(不用struts),在servlet里一样取不到
getAttribute();如果request.getRequestDispatcher("somejsp.jsp").forward(request,response); 给一个jsp页面又可以
之所以取不到有可能是重定向。分为两个请求了。
找不到原因的话改用页面隐藏域提交 action里request.getParameter("")吧。
用request.getRequestDispatcher("servlet/或jsp").forward(request,response); 在 servlet 或 struts 的 action 或 jsp 都可以用getAttribute() 得到值。不好意思
恩。之前收不到的原因就是因为没有转发过去。但是转发语句写在<% %>里,页面就直接跳转了。得写js来控制
window.location.href="user.do";写我试过会跳转,可是不会把attribute带过去