VUE + ElementUI传入后端的值为null或者“ “(空值)时后端怎么处理--java后端

1.在java开发中前端传入后端的值为空时常常出现空指针异常,这种情况往往是前端传入控制,后端使用toString()进行转换时出现空指针异常
2.如果使用String.valueOf()进行转换时得到的结果是“null”,所以在判断的时候需要进行判断,具体请看如下代码:

if (String.valueOf(params.get("pId")) != "null" && String.valueOf(params.get("pId")) != "") {
     //String pId = String.valueOf(params.get("pId"));
     String pId = params.get("pId").toString();
     dyFormVO.setpId(pId);
 }

 if (String.valueOf(params.get("applicant")) != "null" && String.valueOf(params.get("applicant")) != "") {
     //String applicant = String.valueOf(params.get("applicant"));
     String applicant = params.get("applicant").toString();
     dyFormVO.setApplicant(applicant);
 }

但是传入后端保存到数据库是存的值可能是字符串“null”,在后端处理后,但是还有有存入“null”的情况。
这是是添加或者更新时需要注意的
我采用的方法是在mybatis中加入条件判断,insert标签中需要使用if条件判断

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into sys_user(
    user_name,user_password,
    <if test="userEmail != null and userEmail !=''">
        user_email,
    </if>
    user_info,head_img,create_time)
    values(#{userName},#{userPassword},
    <if test="userEmail != null and userEmail !=''">
        #{userEmail},
    </if>
    #{userInfo},#{headImg},#{createTime})
</insert>

上面的表示的是:如果插入的字段为空值时需要在两个地方加入判断条件,而且需要对应。

表:
sys_user(
    user_name,user_password,
    <if test="userEmail != null and userEmail !=''">
        user_email,
    </if>
    user_info,head_img,create_time
    )
values:
values(#{userName},#{userPassword},
    <if test="userEmail != null and userEmail !=''">
        #{userEmail},
    </if>
    #{userInfo},#{headImg},#{createTime})

如果不太理解可以参考mysql的插入语法。
下面是我的程序代码,请参考,有意见请反馈!

<insert id="saveFormData" parameterType="com.pdm.dproject.domain.DyFormVO" useGeneratedKeys="true" keyProperty="id">
      insert into
        dproject_figurenum
	     (
			<if test="module != null and module != 'null' and module != ''">
			  module,
			</if>
			<if test="subType != null and subType != 'null' and subType != ''">
			  sub_type,
			</if>
			<if test="card != null and card != 'null' and card != ''">
				card,
			</if>
			pro_name,
			<if test="pId != null and pId != 'null' and pId != ''">
				p_id,
			</if>
			<if test="applicant != null and applicant != 'null' and applicant != ''">
				applicant,
			</if>
			<if test="applyDate != null and applyDate != 'null' and applyDate != ''">
				apply_date,
			</if>
			fig_num
	    )
	    values
	      (
			<if test="module != null and module != 'null' and module != ''">
				#{module},
			</if>
			<if test="subType != null and subType != 'null' and subType != ''">
				#{subType},
			</if>
			<if test="card != null and card != 'null' and card != ''">
				#{card},
			</if>
			#{pName},
			<if test="pId != null and pId != 'null' and pId != ''">
				#{pId, jdbcType=VARCHAR},
			</if>
			<if test="applicant != null and applicant != 'null' and applicant != ''">
				#{applicant, jdbcType=VARCHAR},
			</if>
			<if test="applyDate != null and applyDate != 'null' and applyDate != ''">
				#{applyDate},
			</if>
		    #{figNum}
	      )
</insert>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用VueElement UI来显示后端图片,可以按照以下步骤进行操作: 1. 在Spring Boot项目中,创建一个用于存储图片的文件夹,比如将其命名为"images"。 2. 将需要显示的图片上传到该文件夹中。 3. 在后端编写一个Controller,用于处理图片请求。可以添加一个接口,比如"/api/image/{imageName}",用于获取某个特定图片的路径。 ```java @RestController @RequestMapping("/api/image") public class ImageController { @Value("${image.path}") private String imagePath; @GetMapping("/{imageName}") public void getImage(@PathVariable String imageName, HttpServletResponse response) throws IOException { File file = new File(imagePath + "/" + imageName); FileInputStream fis = new FileInputStream(file); IOUtils.copy(fis, response.getOutputStream()); } } ``` 配置文件中的image.path属性需要指向存放图片的文件夹路径。 4. 在Vue中,使用Element UI的表格组件来显示图片。首先在Vue组件中定义一个data属性,用于保存图片数据。 ```javascript data() { return { imageUrl: '/api/image/' tableData: [] }; } ``` "imageUrl"属性用于指定获取图片的接口路径。 5. 从后端获取图片数据,在Vue组件的"mounted"生命周期中发起一个请求,获取后端返回的图片数据,并将其赋给表格的数据属性。 ```javascript mounted() { axios.get('/api/imageData') .then(response => { let data = response.data; // 对返回的data进行处理,将图片路径拼接上指定的路径 data.forEach(item => { item.imageUrl = this.imageUrl + item.imageName; }); this.tableData = data; }) .catch(error => { console.error('Error:', error); }); } ``` 6. 在表格的列定义中,使用Element UI的"el-table-column"组件来显示图片。 ```html <el-table-column prop="imageUrl" label="图片"> <template slot-scope="scope"> <img :src="scope.row.imageUrl" width="100" height="100"> </template> </el-table-column> ``` 通过以上的步骤,就可以在Spring Boot中使用VueElement UI来显示后端图片了。在表格中的图片列中,会显示相应的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值