读书人

有个有关问题请大神帮助解决一下?

发布时间: 2013-12-26 00:35:35 作者: rapoo

有个问题请大神帮助解决一下??
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>内容管理系统之用户注册</title>
<style type="text/css">
<!--
.style1{
color:#FF0000
font-weight:bold
}
-->
</style>

</head>

<body>
<!-- 该表单的提交要 通过regedit.action-->

<form name="user" action="regedit.action" method="post">
<table width="776" border="1">
<tr>
<td colspan="2">
<div align="center">
用户注册【<span class="style1">消息提示:${regedit.msg} </span>】
</div>
</td>
</tr>

<tr>
<td>
<div align="right">
用户名:
</div>
</td>
<td>
<input type="text" name="username" value="${user.username}"/>
</td>
</tr>
<tr>
<td>
<div align="right">
密码:
</div>
</td>
<td>
<input type="password" name="password" value="${user.password} "/>
</td>
</tr>
<tr>
<td colspan="2">

<div align="center">
<input type="submit" name="submit" value="提交">

<input type="reset" name="Submit" value="重置"/>
</div>
</td>
</tr>
</table>
</form>

</body>
</html>
这是我的JSP页面,为注册页面
下面是RegeditAction.java
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.myEdition.bean.User;
import com.myEdition.dao.UserDAO;
import com.myEdition.dao.impl.UserDAOImpl;
import com.myEdition.domain.Regedit;
import com.myEdition.domain.impl.Regeditlmpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("RegeditAction")

@Scope("prototype")
public class RegeditAction extends ActionSupport {

//定义接口Regedit
@Resource

private User user;
private Regeditlmpl RegeditImpl;

public User getUser(){
return user;
}

public void setUser(User user){
this.user=user;

}
public String execute(){
//保存用户提交的信息
User user1 = RegeditImpl.find(user.getPassword(), user.getUsername());
if(user!=null){
Map session=(Map)ActionContext.getContext().getSession();
session.put("user1", user1);
RegeditImpl.saveUser(user);
//User user1 = RegeditImpl.find(user.getPassword(), user.getUsername());

return "regedit";
//返回页面
}
else
return null;

}
public Regeditlmpl getRegeditImpl() {
return RegeditImpl;
}

public void setRegeditImpl(Regeditlmpl regeditImpl) {
RegeditImpl = regeditImpl;
}

为什么User user1 = RegeditImpl.find(user.getPassword(), user.getUsername());会是空指针呢?怎样才能获得JSP页面提交的参数呢?


[解决办法]
<input type="text" name="username" value="${user.username}"/>
改成
<input type="text" name="user.username" value="${user.username}"/>

密码一样的道理。改下
[解决办法]
两种解决方式:
一种像1L所说;
第二种在action中 implements ModelDriven<User>并覆写
public User getModel() {
return this.model;
}去掉private User user;和user的set、get方法
[解决办法]

//定义接口Regedit
@Resource

放到
private Regeditlmpl RegeditImpl;上面

如:
private User user;
//定义接口Regedit
@Resource
private Regeditlmpl RegeditImpl;

前台按1L所说修改!
[解决办法]
关键哪是空指针,user=null?
还是find方法里的某句空指针?
[解决办法]

引用:
Quote: 引用:

两种解决方式:
一种像1L所说;
第二种在action中 implements ModelDriven<User>并覆写
public User getModel() {
return this.model;
}去掉private User user;和user的set、get方法
我是按楼那样做的,usename和password值可以接收到,但是我调试发现Id为空,数据库我设的id为自动增长,最后报错RegeditImpl.saveUser(user);为空指针,这是可能是什么原因造成的呢?求大神指点!!
和id没关系,是因为RegeditImpl这个为null,造成的,你spring相关注解写错了
[解决办法]
//定义接口Regedit
@Resource

private User user;
private Regeditlmpl RegeditImpl;

这个你还是没有改! 你把@Resource 放到
private Regeditlmpl RegeditImpl; 上面! 不用注入User对象,应该注入DAO或Manager层!
改成

private User user;
@Resource
private Regedit RegeditImpl; //这里最好用接口,而不用具体实现类!

[解决办法]
private Regeditlmpl RegeditImpl; 红色部分不要直接用具体实现类,应该使用接口!
再不行你把@Resource 改成@Autowired 试试!
[解决办法]
你是想用注解bean还是手动注入bean!
如果注解把
<context:component-scan base-package="com.myEdition"></context:component-scan>
不要注释!
把下面的那些
<bean id="UserDAOImpl" class="com.myEdition.dao.impl.UserDAOImpl">
这些类全部给注释掉!!!!

在Regeditimpl中的saveUser()方法中加一个断点,看能进去不!

读书人网 >J2EE开发

热点推荐