一对多、表外键处理时Action中的No result defined for action cn.myssh.contact.action.ContactAction and re

---- HTTP Status 404 - No result defined for action cn.myssh.contact.action.ContactAction and result input

----Error creating bean with name 'sessionFactory' defined in class path resource [bean1.xml]: Invocation of init method failed;

 环境:

Client表(One) 主键(cID)

Contact表(Many) 外键(custId)



不知道该从哪里开始说起,因为在自己学习SSH的时候,在进行表外键的Action处理时,出现了多种问题,在这里自己整理一下。

下面的主要都是在一对多,表外键时发生的问题,其它的不一定适用。

1、No result defined for action cn.myssh.contact.action.ContactAction and result input

-首先,要确认你在对应的action文件中,写上了对应的action方法;以及在struts.xml文件中写了对应的result.

这个问题,有可能是以下两种原因造成的。

·在action文件中,使用了底层方法来进行获取外键set的属性。如下面的代码

ContactAction中的 addSubmit方法

	public class ContactAction extends ActionSupport implements ModelDriven<Contact>{
	//使用模型驱动获取对象
	private Contact contact = new Contact();
	public Contact getModel() {
		return contact;
	}
	
	private ContactService contactService;
	
	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}
	
	//提交添加
	public String addSubmit(){
		//原始方法
		//获取网页中传回的cid值
		String scid = ServletActionContext.getRequest().getParameter("cID");
		int cid = Integer.parseInt(scid);
		
		//创建新client对象
		Client client= new Client();		<--注意,这里创建了一个client对象
		client.setcID(cid);
		
		//将client对象放到contact中
		contact.setClient(client);
		
		contactService.addSubmit(contact);
		return "addSubmit";
	}


jsp页面上的内容

	<td>
            		<select name="cID" type="select">		<-- name="cID"
            			<c:forEach items="${cList }" var="clientList">
            				<option value="${clientList.cID}" >${clientList.clientName}</option>
            			</c:forEach>
            		</select>
	</td>


此时,我在Contact类中的内容中,也创建了个client对象:

public class Contact {


	private int contactId;
	private String contactName;
	private String gender;
	private int age;
	private String mobile;
	private String phoneNum;
	//private Date updateTime;
	
	//创建客户联系
	private Client client = new Client();		<--注意,这里也创建了一个client
	
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
在这个情况下,运行这个底层方法,就会出现上面的错误。

######但是,如果使用以下的便捷方法,即便Contact类写成上面的样式,也不会出现错误。


(1).在jsp页面上进行修改

	<td>
            		<select name="client.cID" type="select">	<-- name="client.cID", "client"要与Contact实体类中的Client对象名一致
            			<c:forEach items="${cList }" var="clientList">
            				<option value="${clientList.cID}" >${clientList.clientName}</option>
            			</c:forEach>
            		</select>
	</td>

(2).在ContactAction中修改方法成以下就可以。

//提交添加
	public String addSubmit(){
		
		contactService.addSubmit(contact);
		return "addSubmit";
	}

####如果想要用底层方法来实现上面的action的话,记得要将Contact实体类中的外键对象,只进行声明就好,不要创建对象。

即: private Client client;


####另外也不要想两种形式同时存在,因为会造成另外一种错误的出现。

-java.lang.NumberFormatException: null (Console)

-The server encountered an internal error that prevented it from fulfilling this request. (页面)


2、另外一下比较低级的错误

-Error creating bean with name 'sessionFactory' defined in class path resource [bean1.xml]: Invocation of init method failed;

这个错误是在建立联系中,配置出错了,所以会有这个问题。

在Contact类中,写成也以下的样子,就 会出错


public class Contact {

	private int contactId;
	private String contactName;
	private String gender;
	private int age;
	private String mobile;
	private String phoneNum;
	//private Date updateTime;
	
	//创建客户联系
	private int custId;	<--这个地方同,没有引用外键关联的类来创建对象所以sessionFactory无法创建
	
	public int getCustId() {
		return custId;
	}
	public void setCustId(int custId) {
		this.custId = custId;
	}

在一对多中,多的一方的实体类里,要引入关联的类,并声明对象(声明就可以),才可以产生一对多的联系。

将private int custId; 改成 private Client client;

就可以解决上面问题。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值