0

我在我的 struts 2 应用程序中编写了代码,一切正常,但我没有在 succecc.jsp 页面获得属性。请告诉我我在哪里犯错...

我的动作课是

package action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.Empdao;
import model.Empmodel;
public class Empaction extends ActionSupport implements ModelDriven<Object>,SessionAware{

private static final long serialVersionUID = 1L;
    Empmodel modelobj;
    Map<String, Object> map;
    public String execute() throws Exception{
        map.put("a", modelobj.getFirstname());
        Empdao empdao = new Empdao();
        int queryResult = empdao.registration(modelobj);
        if (queryResult==0)
        {   
          addActionError("it is a dublicate entry please enter anothe id");
              return ERROR;
        }   
        else{   
         return SUCCESS;    
         }
}

    @Override
    public Object getModel() {
    modelobj = new Empmodel();
    return null;
    }

    @Override
    public void setSession(Map<String, Object> map) {
        // TODO Auto-generated method stub
        this.map =map;
    }

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
       "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devmode" value="true" />
<package name="loginmodel" extends="struts-default">
    <action name="emplogin" class="action.Empaction">
        <result name="input">/Registration/empregistration.jsp</result>
        <result name="success">/Registration/success.jsp</result>
        <result name="error">/Registration/empregistration.jsp</result>
    </action>
</package>
</struts>

成功.jsp 是

<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    Registration successful
    <s:property value="#session.a" />
</body>
</html>

Empmodel 类

package model;

public class Empmodel {
private String firstname;
private String lastname;
private String id;
private String gender;
private String dob;
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int  pincode;
private long mobileno;
private String groups;
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
public String getMaritalstatus() {
    return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
    this.maritalstatus = maritalstatus;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getJoiningdate() {
    return joiningdate;
}
public void setJoiningdate(String joiningdate) {
    this.joiningdate = joiningdate;
}
public String getDesignation() {
    return designation;
}
public void setDesignation(String designation) {
    this.designation = designation;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}


public long getMobileno() {
    return mobileno;
}
public void setMobileno(long mobileno) {
    this.mobileno = mobileno;
}
public String getGroups() {
    return groups;
}
public void setGroups(String groups) {
    this.groups = groups;
}
public int getPincode() {
    return pincode;
}
public void setPincode(int pincode) {
    this.pincode = pincode;
}

}
4

2 回答 2

2

首先你需要纠正你的行为,使用的想法ModelDriven是将你的模型推入堆栈,你正在推null. 如果这就是你使用它的全部,你
也可以摆脱它。SessionAware

public class Empaction extends ActionSupport implements ModelDriven<Empmodel> {

    private static final long serialVersionUID = 1L;
    Empmodel modelobj = new Empmodel();

    public String execute() throws Exception {

        Empdao empdao = new Empdao();

        int queryResult = empdao.registration(modelobj);

        if (queryResult == 0) {
            addActionError("it is a dublicate entry please enter anothe id");
            return ERROR;
        } else {

            return SUCCESS;
        }
    }

    @Override
    public Empmodel getModel() {
        return modelobj;
    }
}

现在您Empmodel位于顶部,ValueStack因此这将起作用:

<s:property value="%{firstName}"/>

确保ModelDrivenInterceptor在您的拦截器堆栈上。

于 2013-02-23T12:48:21.203 回答
0

为什么使用 session,如果你可以使用 valuestack 来实现,你有很多方法可以实现

使用 ognl

<s:property value="modelobj.firstname "/>

使用小脚本

<%
String fName=(String)session.getAtribute("a");

%>
于 2013-02-23T12:39:40.903 回答