shiwenzhicc
一个智子
采纳率75%
2017-07-17 13:49 浏览 3.8k

java.lang.NumberFormatException: For input string

2

直接在浏览器访问tolist.do,这是一个列出数据库帖子的请求,测试可以查询获取到pList中,但是执行controller中的model.addAttribute("pList", pList);这句就会报错,注释掉就没有报错。
求大神解答,万分感谢!!!

报错信息:
图片说明

还有debug信息:

 DEBUG [http-bio-8080-exec-3] - Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [index.jsp]] in DispatcherServlet with name 'springmvc'
DEBUG [http-bio-8080-exec-3] - Added model object 'pList' of type [java.util.ArrayList] to request in view with name 'null'
DEBUG [http-bio-8080-exec-3] - Forwarding to resource [index.jsp] in InternalResourceView 'null'
七月 17, 2017 9:36:36 下午 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: "title"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)

mapper.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.edu.mapper.PostMapper">
    <resultMap type="post" id="postResult">
        <id property="id" column="pid"></id>
    </resultMap>
    <insert id="insert" parameterType="post" useGeneratedKeys="true" keyProperty="id">
        insert into t_post values(null,#{title},#{author},#{content},now())
    </insert>
    <select id="findAll" resultMap="postResult">
        select * from t_post 
    </select>
    <select id="findById" parameterType="Integer" resultMap="postResult">
        select * from t_post where pid = #{id}
    </select>
    <select id="findByTitle" parameterType="String" resultMap="postResult">
        select * from t_post where title = #{title}
    </select>
</mapper>

service

 package cn.edu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.mapper.CommentMapper;
import cn.edu.mapper.PostMapper;
import cn.edu.pojo.Comment;
import cn.edu.pojo.Post;
import cn.edu.pojo.PostComment;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapperImpl;
    @Override
    public String insert(Post post) {
        Integer id = postMapperImpl.insert(post);
        String msg = "1";
        System.out.println(id);
        if(id == null){
            msg = "0";
        }
        return msg;
    }

    @Override
    public List<Post> findAllPosts() {
        return postMapperImpl.findAll();

    }

}

controller

 package cn.edu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.pojo.Comment;
import cn.edu.pojo.Post;
import cn.edu.service.PostService;

@Controller
public class PostController {
    @Autowired
    private PostService postServiceImpl;

    @RequestMapping("tolist")
    public String toList(Model model){
        List<Post> pList = postServiceImpl.findAllPosts();
        model.addAttribute("pList", pList);
        return "forward:index.jsp";
    }

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答 默认 最新

  • Sun1956 ps45221 2017-07-18 00:56

    title格式错误,检查数据库、pojo、mapper、jsp中的类型
    此外forward:index.jsp这个写法好像有问题,找资料确认下

    点赞 评论 复制链接分享
  • strutce 丵鹰 2017-07-18 01:42

    有一个叫title的字符串你格式化为int类型出错了,你找下哪里使用数字格式化,看下是否出现非数值的字符串

    点赞 评论 复制链接分享
  • qq_34088929 sugarMei 2017-07-18 06:02

    title不能转换为integer,传值的问题吗

    点赞 评论 复制链接分享
  • xuanzhangran 转角人生 2017-07-18 07:30

    看看你在哪里用了title。对title操作了什么。

    点赞 评论 复制链接分享

相关推荐