java ee中jsp如何向struts2传递参数

这是jsp传递参数的页面:

这是struts的action里接收参数,并插入数据库的代码:

我知道的这里的getXXX方法不能获取jsp传递过来的参数,所以在控制台输出的参数都是null
空指针异常:第一行就是null

应该怎么改呢?

第1个回答  2015-05-18
1、jsp页面中textfield的value写成student.bjmc student.xh的形式,把你想传的值放在textfield的value属性中,而不是写在中间
2、在action中定义一个student的私有的成员变量,添加get和set方法。
3、在student中直接使用这个student就可以了。

你上面的写法 首先前台参数没有传递到后台,其次你的student是自己new出来的。里面根本没有值。

目测你的代码一场出现在这一行:if(bjmc.equal(s1)).
因为student里没有值,所有bjmc为null,所以这儿报空指针异常了。
第2个回答  推荐于2016-10-23
你不能new Student来获取参数啊。
struts2 框架可以用注入的方式传参。就是说你只需要在action类里写上
private Student student;
public void setStudent(Student student){
this.student = student;

}
然后把你的jsp页面上改为 name=“student.bjmc”,name=“student.xh”....本回答被提问者采纳
第3个回答  2015-05-18
Struts中不是有OGNL表达式吗?
第4个回答  2015-05-17
<FORM id=adminlogin method=post
name=adminlogin action="user!login">

<package name="studentInfo" namespace="/" extends="struts-default">
<action name="user" class="com.java1234.action.UserAction">
<result name="error">/login.jsp</result>
<result name="success">/main.jsp</result>
</action>追问

什么意思,应该怎么改。我看不懂

追答

public String login(){
HttpSession session=request.getSession();
if(StringUtil.isEmpty(user.getUserName())||StringUtil.isEmpty(user.getPassword())){
error="用户名或密码为空!";
return ERROR;
}
if(StringUtil.isEmpty(imageCode)){
error="验证码为空!";
return ERROR;
}
if(!imageCode.equals(session.getAttribute("sRand"))){
error="验证码错误!";
return ERROR;
}
Connection con=null;
try{
con=dbUtil.getCon();
User currentUser=userDao.login(con, user);
if(currentUser==null){
error="用户名或密码错误!";
return ERROR;

追问

你这回答不是我上面的问题

追答

给你做个参考啊

追问

我这个是用jsp+struts2插入记录,相差的也太远了吧,怎么做参考, 我那个是空指针异常,应该怎么改

追答

我上次就是你这种想法。不知道提问的那个人知道没

相似回答