求助!struts1.2的action只forward跳转不执行调用方法!

加嘞个油 2011-10-27 11:35:33
如题啊~ 程序不报错,xml配置应该也没问题,要不肯定报错了。

这是action的代码。

注 : showall方法可以被调用并正确显示在jsp上,而save方法则只forward “保存成功”消息和跳转页面,实际并没有添加数据到数据库里!奇怪就奇怪在一个方法可以执行一个不可以!求解答!!!

public class AllNewsAction  extends DispatchAction

{
public ActionForward addnews(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
NewsForm AllNewsActionForm=(NewsForm)form;
BasicDataSource ds = (BasicDataSource) getDataSource(request,"struts");
try
{
ActionNews actionNews= new ActionNews(AllNewsActionForm,ds);
if(((NewsForm) form).getNews_id().trim().equals(""))
throw new Exception("编号不允许为空!");
else if (((NewsForm) form).getNews_id().length()>4)
throw new Exception("编号长度最长为4位!");
else
{
actionNews.save();
request.setAttribute("info","保存成功!");
}
}
catch(Exception e)
{
request.setAttribute("info",e.getMessage());
}
return mapping.findForward("save");

}
public ActionForward showall(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
{
NewsForm AllNewsActionForm=(NewsForm)form;
BasicDataSource ds = (BasicDataSource) getDataSource(request,"struts");
try
{
ActionNews actionNews= new ActionNews(AllNewsActionForm,ds);
List<String[]>result=actionNews.search();
if(result.size()>0)
{
request.setAttribute("result",result);
request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size()));
}
else
request.setAttribute("info_all", "没有符合要求的记录!");

}
catch(Exception e)
{
request.setAttribute("info_all",e.getMessage());
}
return mapping.findForward("searchAll");
}



具体的actionform类代码,请教看哪里错了。

public class ActionNews 
{
public NewsForm form;
public Connection conn;

public ActionNews(NewsForm form,DataSource ds) throws Exception
{
super();
this.form=form;
this.conn = ds.getConnection();
}
public List<String[]>search() throws Exception
{
List<String[]> result = new LinkedList<String[]>();
String sql="select * from s_news";
PreparedStatement pstmt= conn.prepareStatement(sql);
ResultSet rs =pstmt.executeQuery();
while(rs.next())
{
String[] row=new String[6];
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = rs.getString(3);
row[3] = rs.getString(4);
row[4] = rs.getString(5);
row[5] = rs.getString(6);
result.add(row);
}
rs.close();
conn.close();
return result;
}
public void save()throws Exception
{
try
{
String newsid=form.getNews_id();
String newstheme=form.getNews_theme();
String newsauthor=form.getNews_author();
String newsdate=form.getNews_date();
String newsdetail=form.getNews_detail();
String newsurl=form.getNews_url();
String sql="insert into s_news values('"+newsid+"','"+newstheme+"','"+newsauthor+"','"+newsdate+"','"+newsdetail+"','"+newsurl+"')";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate(sql);
pstmt.close();
conn.close();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}


部分xml代码:
  <global-forwards> 
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />
</global-forwards>
<action-mappings>
<action name="AllNewsActionForm" path="/AllNewsAction" scope="request" type="action.AllNewsAction" parameter="method">
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />
</action>



部分jsp代码:
		//这个form可以查询数据库里现有所有的数据
<html:form action="AllNewsAction"><input type=hidden name="method" value="showall">
<html:submit value="查询所有记录 " />
</html:form>


//这个增加却不行,求解答。

<html:form action="AllNewsAction"><input type=hidden name="method" value="addnews">
<table width=100%>
<tr>
<td align="center">
编号:
<html:text property="news_id" maxlength="4" />
<p>
日期:
<html:text property="news_date" />
<br><br>链接:
<html:text property="news_url" />
<br><br>
</td>
</tr>
<tr>
<td align="center">
<html:submit value="提交" />
  
<html:reset value="重置" />
  

</td>
</tr>
</table>
</html:form>
...全文
195 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zenki520 2011-10-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mscf 的回复:]
是数据库事务没提交吧,乍一看已经是很标准的用法了
[/Quote]

貌似你这个问题?defaultAutoCommit?看看值
加嘞个油 2011-10-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mscf 的回复:]
是数据库事务没提交吧,乍一看已经是很标准的用法了
[/Quote]

谢谢!结贴!
加嘞个油 2011-10-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wxxy20071547 的回复:]
我晕 大哥 你不知道debug一下 很简单的问题啊 debug很快就可以找到错误
可能是页面form没取到值,保存到数据库就是空的 只是猜测,你自己打断点Debug
[/Quote]

找到错了,是事务的问题
风影simple 2011-10-28
  • 打赏
  • 举报
回复
我晕 大哥 你不知道debug一下 很简单的问题啊 debug很快就可以找到错误
可能是页面form没取到值,保存到数据库就是空的 只是猜测,你自己打断点Debug
加嘞个油 2011-10-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 trocp 的回复:]
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />

查询时转到/index.jsp页面,save时转到/message.jsp页面,不知道两个页面是否都显示了提示信息,
比如/message.jsp应该显示info,${info}
[/Quote]

都设置了显示了提示信息,但是点保存时,跳转到message页面只有保存成功,而实际没有存入数据库,查询则什么都不提示。
Phoenix Slade 2011-10-28
  • 打赏
  • 举报
回复
<form-bean name="fmBusinessForm" type="com.reg.form.fmBusinessForm">

<!-- 零售市场-商户登记表_数据保存和数据列表检索 su 20090616 -->
<action path="/fmBusinessSave"
type="org.springframework.web.struts.DelegatingActionProxy" name="fmBusinessForm"
scope="request">
<forward name="success" path="/nmsc/fmBusiness/fmBusinessSearchList.jsp"/>
<forward name="succ" path="/fmBusinessUpdateInit.do"/>


</action>
oO临时工Oo 2011-10-28
  • 打赏
  • 举报
回复
<forward name="searchAll" path="/index.jsp"/>
<forward name="save" path="/message.jsp" />

查询时转到/index.jsp页面,save时转到/message.jsp页面,不知道两个页面是否都显示了提示信息,
比如/message.jsp应该显示info,${info}
薛定谔之死猫 2011-10-28
  • 打赏
  • 举报
回复
是数据库事务没提交吧,乍一看已经是很标准的用法了
加嘞个油 2011-10-27
  • 打赏
  • 举报
回复
求解答啊,还有我如果把这个AllNewsAction类的增删改查都分开写,也都可以执行,代码都不用改。合并了增,改,删却不行了,郁闷啊! 哪里代码错了,求解答~估计参数传错了。~~

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧