我正在研究Struts2和hibernate集成。它显示无法在命名空间中实例化操作的错误。我怎么解决它

时间:2014-02-27 05:31:40

标签: hibernate struts2

我无法插入其显示的值,无法在命名空间'/ null'

中实例化操作

我的pojo类,我声明变量的模型并创建表

package com.konzern.pojo;

import java.io.Serializable;

import javax.persistence.*;

@Entity

@Table(name = "register")

public class Register implements Serializable {

@Id @GeneratedValue (strategy=GenerationType.AUTO)

 @Column(name = "UserId")

private int UserId;

 @Column(name = "UserName")

 private String UserName;

 @Column(name = "Designation")
 private String Designation;

 @Column(name = "DOJ")
 private String DOJ;

 @Column(name = "DOB")
 private String DOB;



 @Column(name = "Address")
 private String Address;

 @Column(name = "Phone")
 private String Phone;

 @Column(name = "Blood")
 private String Blood;

 @Column(name = "Email")
 private String Email;

 @Column(name = "EmpPassword")
 private String EmpPassword;

public int getUserId() {
    return UserId;
}

public void setUserId(int userId) {
    UserId = userId;
}

public String getUserName() {
    return UserName;
}

public void setUserName(String userName) {
    UserName = userName;
}

public String getDesignation() {
    return Designation;
}

public void setDesignation(String designation) {
    Designation = designation;
}

public String getDOJ() {
    return DOJ;
}

public void setDOJ(String dOJ) {
    DOJ = dOJ;
}

public String getDOB() {
    return DOB;
}

public void setDOB(String dOB) {
    DOB = dOB;
}

public String getAddress() {
    return Address;
}

public void setAddress(String address) {
    Address = address;
}

public String getPhone() {
    return Phone;
}

public void setPhone(String phone) {
    Phone = phone;
}

public String getBlood() {
    return Blood;
}

public void setBlood(String blood) {
    Blood = blood;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

public String getEmpPassword() {
    return EmpPassword;
}

public void setEmpPassword(String empPassword) {
    EmpPassword = empPassword;
}


}

controller类,我从jsp页面获取值

package com.konzern.controller;


import java.net.URL;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.SessionFactory;


import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Map;

import com.konzern.operations.RegisterOperations;
import com.konzern.pojo.Login;
import com.konzern.pojo.Register;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterController  extends ActionSupport implements ServletRequestAware,ApplicationAware {

     private static final long serialVersionUID = 1L;

     RegisterOperations ro=new RegisterOperations();

     Register r;

     public Register getR(){
         return r;
     }

     public void setR(Register r){
         this.r=r;

     }
     Login l;

     public Login getL() {
        return l;
    }

    public void setL(Login l) {
        this.l = l;
    }
    HttpServletRequest request;
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;

    }
      public HttpServletRequest getServletRequest() {
            return request;
        }    

      Map m;
        public void setApplication(Map m)
        {
            this.m=m;
        }

      public String insertaction(){
          System.out.println("hhhhhh");
          ro.registration(r);
          ro.insert(l);
          return SUCCESS;

      }

     /* public String tolog(){
          System.out.println("hhhhhh");
          ro.insert(l);
          return SUCCESS;

      }*/

    @Override
    public void setApplication(java.util.Map<String, Object> arg0) {
        // TODO Auto-generated method stub

    }

struts.xml中

 <?xml version="1.0" encoding="UTF-8"?>

        <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

        <struts>
            <include file="struts-default.xml"/>




            <constant name="struts.custom.i18n.resources"
                value="ApplicationResources" />

            <package name="a" extends="struts-default">

          <!--   <interceptors>
                    <interceptor class="com.konzern.loginInterceptor" name="loginInterceptor"/>
                    <interceptor-stack name="loginStack">
                        <interceptor-ref name="loginInterceptor"/>
                        <interceptor-ref name="defaultStack"/>
                    </interceptor-stack>                 
                </interceptors>      -->

               <!--  <default-interceptor-ref name="loginStack"/>

                <global-results>  
                    <result name="login">/login.jsp</result>                        
                </global-results> --> 

                <action name="verify" class="com.konzern.controller.LoginController" method="LoginAction">
                  <result name="success">/Newlogin.jsp</result>
                </action>
                 <action name="login" class="com.konzern.controller.LoginController" method="indexAction">
                      <result name="success">/Admin.jsp</result>
                      <result name="login">/User.jsp</result>
                    <result name="error">/Newlogin.jsp</result>
                </action> 
                <action name="logout" class="com.konzern.controller.LoginController" method="logoutAction">
                  <result name="success">/Newlogin.jsp</result>
                </action>
                <action name="myjobs" class="com.konzern.controller.LoginController" method="LoginAction">
                <result name="success">/Attendance.jsp</result>
                </action>
                 <action name="Register" class="com.konzern.controller.RegisterController" method="insertaction">
                <result name="success">/Newlogin.jsp</result>
                </action>


            </package>
        </struts>

        stack trace:

输入例外报告

        message Unable to instantiate Action, com.konzern.controller.RegisterController, defined for 'Register' in namespace '/'null

        description The server encountered an internal error that prevented it from fulfilling this request.

        exception

        Unable to instantiate Action, com.konzern.controller.RegisterController,  defined for 'Register' in namespace '/'null
            com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)
            com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
            com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
            org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
            org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
            com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
            org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
            org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
        root cause

        java.lang.NullPointerException
            com.konzern.operations.RegisterOperations.<init>(RegisterOperations.java:17)
            com.konzern.controller.RegisterController.<init>(RegisterController.java:27)
            sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
            java.lang.reflect.Constructor.newInstance(Unknown Source)
            java.lang.Class.newInstance0(Unknown Source)
            java.lang.Class.newInstance(Unknown Source)
            com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
            com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:182)
            com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:171)
            com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:22)
            com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:141)
            com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
            com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
            com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
            org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
            org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
            com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
            org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
            org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)

的login.jsp

> <%@ taglib prefix="s" uri="/struts-tags" %> <%@ page
> import="java.util.*;" %>
> 
> <html>
> 
> <head> <meta charset="utf-8"> <title>Konzern</title> <link
> href="style/css.css" rel="stylesheet" type="text/css"> </head>
> 
> <body> <div class="header_full">
>       <div class="header">
>           <div class="Logo_holder"></div>            
>           </div> </div> <!--HEADER ENDS HERE-->
> 
> <div class="current_username_full">   <div
> class="current_username_holder">Log In</div> </div>
> 
> <!--USERNAME ENDS HERE-->
> 
> <div class="options_holder_full">     <div class="options_holder">
>       <div class="log_in_holder">
>           <div class="ADMIN_login">
>               <s:form action="login">
>           <h1>Log in here</h1>
>                 <label></label>
>             
>                 <s:textfield label="Username" name="b.userid" cssClass="inputforlogin"/>
>                 <br></br>
>                 <s:password label="Password" name="b.password" cssClass="inputforlogin"/>
>                
>                <s:submit value="login" cssClass="button" />
>                 
>                 
>                 </s:form>
>                 <p style="float:left; margin-top:2px; margin-left:120px;"><a href="Register.jsp">Register here</a></p>
>             </div>          
>         </div>
>     </div> </div> <!--OPTIONS_HOLDER_FULL-->
> 
> <div class="footer_full"></div>
> 
> 
> </body> </html>

register.jsp

<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page
> import="java.util.*;" %> <html> <head> <meta charset="utf-8">
> <title>Konzern</title> <link href="style/css.css" rel="stylesheet"
> type="text/css"> </head>
> 
> <body> <div class="header_full">
>       <div class="header">
>           <div class="Logo_holder"></div>            
>           </div> </div> <!--HEADER ENDS HERE-->
> 
> 
> 
> 
> 
> <div class="current_username_full">   <div
> class="current_username_holder">Register new user</div> </div>
> 
> <!--USERNAME ENDS HERE-->
> 
> 
> 
> <div class="options_holder_full">     <div class="options_holder">
>       <div class="register_holder">
>           <div class="register">
>               <h1>Register here</h1>
>               <s:form action="Register">
>               
>               <s:textfield name="r.UserName" label="Name" cssClass="input"/>
>               <s:textfield name="r.Designation" label="Designation" cssClass="input"/>
>               <s:textfield name="r.DOJ" label="Date of Join" cssClass="input"/>
>               <s:textfield name="r.DOB" label="Date of Birth/Age" cssClass="input"/>
>               <s:textarea  name="r.Address" label="Address" cssClass="textarea"/>
>               <s:textfield name="r.Phone" label="Mobile Number" cssClass="input"/>
>               <s:textfield name="r.Blood" label="Blood Group" cssClass="input"/>
>               <s:textfield name="r.Email" label="E-mail" cssClass="input"/>
>               <s:textfield name="r.EmpPassword" label="Password" cssClass="input"/>
>               
>               <s:submit value="Register" cssClass="button" />
>             
>               </s:form>
>             
> 
>             </div>
>             
>         </div>
>     </div> </div> <!--OPTIONS_HOLDER_FULL-->
> 
> <div class="footer_full"></div>
> 
> 
> </body> </html>

1 个答案:

答案 0 :(得分:0)

我认为问题在于registercontroller类,因为它在该类中显示异常。

  1. 尝试从struts.xml中删除注册操作,并检查所有其他操作类是否正常工作。
  2. 发布jsp文件。
  3. 您可以尝试使用模型驱动获取寄存器类对象 方法
  4. 更改您的类以实现如下所示的模型驱动界面。

    public class RegisterController  extends ActionSupport implements ServletRequestAware,ApplicationAware,ModelDriven{}
    

    并在课程的第一行初始化如下所示的寄存器类。

    Register r = new Register();
    

    然后最终在你的类中实现getModel方法,如

    public Object getModel() {
        return r;
    }