日期:2014-05-20  浏览次数:20915 次

关于struts+hibernate提交数据的问题
有段程序不太明白,每次我把加粗的部分,换成 "request.setAttribute("msg", "<font color=red>"+username+"</font>"+" 注册成功!");"的时候,数据的无法提交了,而且数据返回的页面也是一片空白,返回页面中使用了<bean:write name="msg" />不知道是为什么,请各位指教!!!

action.java:
package com.yourcompany.struts.action;

import java.io.Serializable;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.mine.hiber.Login;
import com.mine.hiber.SessionFactory;
import com.yourcompany.struts.form.LoginForm;

public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
String username=loginForm.getUsername();
String password=loginForm.getSss();
Session session =SessionFactory.getSession();
Login user=new Login(username,password);
session.save(user);
Transaction tx=null;
  tx=session.beginTransaction();

tx.commit();
session.close();
HttpSession sessions = request.getSession();
sessions.setAttribute("msg", "<font color=red>"+username+"</font>"+" 注册成功!");
return mapping.findForward("su");
}
}



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="loginForm" type="com.yourcompany.struts.form.LoginForm" />

  </form-beans>

  <global-exceptions />
  
  <global-forwards >
  <forward name="su" path="/index.jsp" redirect="true"/>

  </global-forwards>

  <action-mappings >
  <action
  attribute="loginForm"
  input="/login.jsp"
  name="loginForm"
  path="/login"
  scope="request"
  type="com.yourcompany.struts.action.LoginAction" />
  <action
  path="/loadmy"
  type="com.yourcompany.struts.action.LoadmyAction"
  validate="false" >
  <forward name="mysu" path="/my.jsp" redirect="true"/>
  </action>


  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>



------解决方案--------------------
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="com.yourcompany.struts.action.LoginAction" /> 
红色的改成 scope="session"试试。
------解决方案--------------------