gpt4 book ai didi

java - 用于获取 success.jsp 页面上的值的 ModelDriven 接口(interface)

转载 作者:行者123 更新时间:2023-12-02 07:15:01 25 4
gpt4 key购买 nike

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

我的 Action 类是

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>

success.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;
}

}

最佳答案

首先,您需要纠正您的操作,使用 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位于您的拦截器堆栈上。

关于java - 用于获取 success.jsp 页面上的值的 ModelDriven 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15040364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com