当前位置:编程学习 > JAVA >>

Struts2 非标签拿不到jsp页面的值 (userid打印为null),不 清楚 为什么 拿 不到 ,请 大侠指点迷津 。

package com.lzg.actions;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

import org.apache.struts2.ServletActionContext;

import com.lzg.dao.PersonDao;
import com.lzg.daoImpl.PersonDaoImpl;
import com.lzg.entity.Person;
import com.opensymphony.xwork2.ActionContext;

public class ListAction {
private String userid;
private String username;
private String gender;
private String password;
private String tele;
private String email;
private String birthday;
private String country;
private String province;
private Date regtime;
private Date modtime;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTele() {
return tele;
}
public void setTele(String tele) {
this.tele = tele;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public Date getRegtime() {
return regtime;
}
public void setRegtime(Date regtime) {
this.regtime = regtime;
}
public Date getModtime() {
return modtime;
}
public void setModtime(Date modtime) {
this.modtime = modtime;
}
public PersonDao getPdao() {
return pdao;
}
public void setPdao(PersonDao pdao) {
this.pdao = pdao;
}
private PersonDao pdao;

                  public String add(){
pdao=new PersonDaoImpl();
ActionContext ac= ActionContext.getContext();
Map Session =ac.getSession();
HttpServletRequest request= ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
try {
p=new Person();
System.out.println(request.getParameter("userid"));
p.setUserid(Integer.parseInt((userid+" ").trim()) );
p.setBirthday(birthday);
p.setEmail(email);
p.setGender(gender);
p.setModtime(modtime);
p.setPassword(password);
p.setProvince(province);
p.setRegtime(regtime);
p.setUsername(username);
p.setCountry(country);
if(p.getUserid()!=null)
pdao.add(p);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}

}

}





jsp




<html>
<%@ page language="java" import="java.util.*" pageEncoding="gbk" isELIgnored="false" contentType="text/html; charset=gbk"%>

<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

  <script language="javascript">
    function func_submit(){
     if(document.getElementById("userid").value.trim().length==0)
     {
     alert("id 不能为空");
     return false;}
     window.location.href ="select_person";
    
    }
    
    function func_changeCountry(){
      alert("清空省市内容");
    }
    
    function func_province(){
      alert("根据国家内容,修改省市选择内容");
    }
  </script>
<body>
  <form >
    <table border="1">
      <tr>
        <td align="right">用户id:</td>
        <td><input type="text" name="userid" style="width:200px"  id="userid"/></td>
      </tr>
      <tr>
        <td align="right">姓名:</td>
         <td><input type="text" name="username" style="width:200px" /></td>
      </tr>
      <tr>
        <td align="right">性别:</td>
        <td><select name="tele" style="width:200px"><option id="0"></option><option id="1">女</option><option id="2">男</option></select></td>
      </tr>
      <tr>
        <td align="right">密码:</td>
        <td><input type="password" name="password" style="width:200px" /></td>
      </tr>
      <tr>
        <td align="right">电话:</td>
        <td><input type="text" name="tele" style="width:200px" /></td>
      </tr>
      <tr>
        <td align="right">Email:</td>
        <td><input type="text" name="email" style="width:200px" /></td>
      </tr>
      <tr>
        <td align="right">出生日期:</td>
        <td><input type="text" name="birthday" style="width:200px" /></td>
      </tr>
      <tr>
        <td align="right">国家:</td>
        <td><select name="country" style="width:200px" onchange="func_changeCountry()"><option id="0"></option><option id="china">中国</option><option id="usa">美国</option></select></td>
      </tr>
      <tr>
        <td align="right">省市:</td>
        <td><select name="province" style="width:200px" onclick="func_province()"></select></td>
      </tr>
      <tr>
        <td align="right">注册时间:</td>
        <td><input type="text" name="regtime" style="width:200px" /></td>
      </tr>
      <tr align="right">
        <td colspan="2"><a href="#" onclick="func_submit()">提交</a>  <a href="select_person.html">返回</a></td>
      </tr>
    </table>
  </form>
</body>
</html>

配置
<?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>
 <package name="struts" extends="struts-default" >
 <action name="select_person" class="com.lzg.actions.ListAction" method="add">
   <result name="success">/select_person.jsp?userid=document.getElementById(userid).value </result>
   <result name="error">/index.jsp </result>
 </action>
 
 </package>
</struts>    


--------------------编程问答-------------------- Userid为何没有get方法??? --------------------编程问答-------------------- window.location.href ="select_person";  你只是传了action  并没有传递参数过去 自然获取不到值 --------------------编程问答-------------------- 有get方法啊:
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
3楼的哥们:
我是这么传的。。是不是对的?
<result name="success">/select_person.jsp?userid=document.getElementById(userid).value </result>

--------------------编程问答--------------------
引用 3 楼 lzg0414 的回复:
<result name="success">/select_person.jsp?userid=document.getElementById(userid).value </result>


你在struts.xml中传?   

在配置文件中getElementById怎么可以拿到页面的值  


你直接用form的submit来提交    只要input的name跟action中的属性名一样  有get/set 方法就能取到了

你用localtion.href="xx.do?a=a&b=b......" 这样是用get方式提交数据,都显示在地址栏中 ,而且中文乱码问题也不好解决


你就用form action="你的action" mothod="post>......</form> --------------------编程问答--------------------
引用 4 楼 wlf2131 的回复:
引用 3 楼 lzg0414 的回复:
<result name="success">/select_person.jsp?userid=document.getElementById(userid).value </result>



你在struts.xml中传?  

在配置文件中getElementById怎么可以拿到页面的值  


你直接用form的submit来……




首先,,我这里不用submit来提交, 第二我method改成POST了,但是无论在配置文件里的result元素里(可以跳转但是带不上值 );
还是在(window.location.href ="select_person?userid=document.getElementById("userid").value")这个无法跳转;求解。
";
--------------------编程问答--------------------


对javascript不熟  就用自己熟悉的方式去做,这样不打击学习的信心  
--------------------编程问答-------------------- 你直接解析url地址 获得id值试试   --------------------编程问答--------------------
引用 4 楼 wlf2131 的回复:
引用 3 楼 lzg0414 的回复:
<result name="success">/select_person.jsp?userid=document.getElementById(userid).value </result>



你在struts.xml中传?  

在配置文件中getElementById怎么可以拿到页面的值  


你直接用form的submit来……



同意楼上 --------------------编程问答-------------------- submit 可以,我试过。。 --------------------编程问答-------------------- window.location.href ="select_person?userid=document.getElementById("userid").value"

这个url分明是两个字符串组成的...里面的js代码会被当做参数值提交

window.location.href ="select_person?userid="+document.getElementById("userid").value;

这样才能取到值 --------------------编程问答--------------------
引用 10 楼 likunlun_ 的回复:
window.location.href ="select_person?userid=document.getElementById("userid").value"

这个url分明是两个字符串组成的...里面的js代码会被当做参数值提交

window.location.href ="select_person?userid="+document.getElementById("u……


+  这个正解
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,