NutzCN Logo
问答 适配器传递from表单对象 值后台接受不到
发布于 1771天前 作者 qq_412bd180 1172 次浏览 复制 上一个帖子 下一个帖子
标签:

实体类

package io.longview.iib.admin.entity;

public class Pet {
	private int id;
	private String name;
	private int age;
	private String sex;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
}

Module代码

package io.longview.iib.admin.module;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import io.longview.iib.admin.entity.Pet;

@At("/adaptor")
@InjectName
@IocBean
public class AdaptorModule {
	
	@At("/form")
	public void form(@Param("..") Pet pet){
		System.out.println("下面是pet的属性:");
		//System.out.println("pet.getId():"+pet.getId());
		System.out.println("pet.getName():"+pet.getName());
		System.out.println("pet.getAge():"+pet.getAge());
		System.out.println("pet.getSex():"+pet.getSex());
	}
	
	@At("/")
	@Ok("jsp:jsp.admin.adaptorIndex")
	public void index(){
		
	}

}

页面代码

<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<t:adminpage page="testvue" pageTitle="vueAjaxCRUD" pageDesc=".">
	<jsp:attribute name="resources">
		<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/fancytree/skin-bootstrap/ui.fancytree.min.css">
		<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/select2/select2.css">
		<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-tagsinput/bootstrap-tagsinput.css">
		<!-- <link rel="stylesheet" href="${pageContext.request.contextPath}/css/vueCRUD.css"> -->
		<style type="text/css">
		</style>
    </jsp:attribute>
	<jsp:attribute name="bottomScripts">
		<script type="text/javascript">
			
		</script>
   	</jsp:attribute>
   	
   	<jsp:attribute name="bottomScripts">
   	<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/fancytree/jquery.fancytree-all.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/handlebars/handlebars-v4.0.5.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/bootstrap-notify/bootstrap-notify.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/select2/select2.full.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/bootstrap-tagsinput/bootstrap-tagsinput.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/plugins/vue/vue.js"></script>
	
	<script>
		$(document).ready(function() {
			
		});
	</script>
	
	</jsp:attribute>
	
	<jsp:body>
	
		<div class="row" id="app">
			<div class="col-md-12">
				<div class="box box-success">
					<div class="box-header with-border">
	              		<h3 class="box-title">box-header</h3>
	            	</div>
	            	<div class="box-body no-padding">
	                	<!-- box body -->
	                	<form id="from" action="${ctx }/adaptor/form" method="post">
	                		<!-- 宠物id<input type="text" name="pet.id"/>
	                		<br/>
	                		 -->
	                		宠物名字<input type="text" name="pet.name"/>
	                		<br/>
	                		宠物岁数<input type="text" name="pet.age"/>
	                		<br/>
	                		宠物性别<input type="text" name="pet.sex"/>
	                		<br/>
	                		<button type="submit" class="btn btn-info">提交</button>
	                	</form>
	                	<!-- box body -->
	            	</div>
				</div>
			</div>
		</div>
	
	</jsp:body>

</t:adminpage>
8 回复

input name全部带前缀了? 删掉之

那我要是有2个对象呢? 两个对象都有 name

@qq_412bd180 仔细看看文档

来自狂拽炫酷吊炸天的 NutzCN

前缀表单的@Param写法不一样哦

教程上是这么写的

混合方式
值得一说的是,按照这个约定,实际上,一个入口函数,是可以支持多个 POJO 的,也可以写成这样

public String someFunc(    @Param("pid") int petId,
                        @Param("..") Pet pet,
                        @Param("..") Food food){
    ...

那么如果 pet 和 food 都有name字段 表单上的name怎么写?

页面代码

<form id="from" action="${ctx }/adaptor/form" method="post">
	                		宠物id<input type="text" name="id"/>
	                		<br/>
	                		宠物名字<input type="text" name="name"/>
	                		<br/>
	                		宠物岁数<input type="text" name="age"/>
	                		<br/>
	                		宠物性别<input type="text" name="sex"/>
	                		<br/>
	                		<button type="submit" class="btn btn-info">提交</button>
	                	</form>

给你demo代码

@Param("::pet.")Pet pet, @Param("::food.")Food food, 

	                		宠物名字<input type="text" name="pet.name"/>
	                		宠物岁数<input type="text" name="pet.age"/>
	                		宠物性别<input type="text" name="pet.sex"/>
	                		食品名字<input type="text" name="food.name"/>
	                		食品价格<input type="text" name="food.price"/>
	                		食品数量<input type="text" name="food.count"/>
添加回复
请先登陆
回到顶部