tuor啊
2022-03-30 17:40
采纳率: 100%
浏览 85

ssm 后台拿不到前端传的form表单里面东西,用断点发现它们都是null

问题遇到的现象和发生背景

owner.jsp页面的form表单:

<form action="${pageContext.request.contextPath}/owner/addProducts.do?userName=${UserInfo.username}" method="post" enctype="multipart/form-data">

            <!--产品信息-->
            <div class="form-group">
                <label class="col-md-2 title">商品编号</label>
                <input type="text" class="form-control" name="productId"
                       placeholder="商品编号" value="t-001" onblur="sendRequest()" id="productId">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">图片</label>
                <input type="file" class="form-control" name="picture"
                       placeholder="图片" value="">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">商品名</label>
                <input type="text" class="form-control" name="product"
                       placeholder="商品名" value="火龙果">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">价格</label>
                <input type="text" class="form-control" name="price"
                       placeholder="价格" value="25">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">净含量</label>
                <input type="text" class="form-control" name="content"
                       placeholder="净含量" value="1000g">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">产地</label>
                <input type="text" class="form-control" name="site"
                       placeholder="产地" value="河北省">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">发布时间</label>
                <input type="date" class="form-control" name="date"
                       placeholder="发布时间" value="">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">状态</label>
                <input type="text" class="form-control" name="state"
                       placeholder="状态" value="1">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">保质期(天)</label>
                <input type="text" class="form-control" name="expiration_date"
                       placeholder="保质期" value="80">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">描述</label>
                <input type="text" class="form-control" name="description"
                       placeholder="描述" value="11111111111111">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">库存</label>
                <input type="text" class="form-control" name="count"
                       placeholder="库存" value="50">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">类型</label>
                <input type="text" class="form-control" name="kind"
                       placeholder="类型" value="火龙果">
            </div>

            <div class="form-group">
                <label class="col-md-2 title">种类</label>
                <input type="text" class="form-control" name="sort"
                       placeholder="种类" value="新鲜水果">
            </div>

            <div style="display: none">
                <input type="text" name="owner" value="${UserInfo.ownerInfo.id}">
            </div>

            <div style="display: none">
                <input type="text" name="ownerName" value="${UserInfo.ownerInfo.ownerName}">
            </div>

            <div class="or-cart-copun-code" style="padding-left: 70%;padding-top: 50px">
                <button type="submit">保存</button>
                <%--            <button type="button" onclick="history.back(-1);">返回</button>--%>
            </div>

        </form>


controller层(@Autowired都有):

//添加货物
    @RequestMapping("/addProducts.do")
    public String addProducts(Product product, MultipartFile picture) throws Exception{

        ownerService.addProducts(product,picture);
        return "owner";
    }

serviceImp层:

@Override
    public void addProducts(Product product,MultipartFile picture) throws Exception {

        //获取文件的原始名称
        String oname=picture.getOriginalFilename();//例如:121.jpg
        //获取文件的后缀名
        String suffix=oname.substring(oname.lastIndexOf("."));  //  .jpg
        //新的文件名
        String newName= UUID.randomUUID()+suffix;
        try {//D://
            picture.transferTo(new File("C:\\Users\\TUOTUOTUOTUOTUO\\IdeaProjects\\lt_bishe\\lt_web\\src\\main\\webapp\\pages\\assets\\img\\product",newName));
//            C:\Users\TUOTUOTUOTUOTUO\IdeaProjects\lt_bishe\lt_web\src\main\webapp\pages\assets\img\product

//            //2. 将文件名赋值给product
//            product.setPicture(newName);
            //3.调用dao的添加方法
            ownerDao.addProduct(product,newName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

dao层:

/添加商品
    @Insert("insert into product_table(productId,picture,product,price,content,site,date,state,expiration_date,description,count,kind,sort,owner) values (#{productId}," +
            "#{picture},#{product},#{price},#{content},#{site},#{date},#{state},#{expiration_date},#{description},#{count},#{kind},#{sort},#{owner})")
    void addProduct(Product product,String picture) throws Exception;

product对象字段(get set方法和tostring没粘):

private Integer id;//编号
    private String productId;//商品编号
    private String picture;//图片路径
    private String product;//产品名
    private Double price;//价格
    private String content;//净含量
    private String site;//产地
    private Date date;//发布时间
    private Integer state;//商品状态 0下架 1上架
    private String stateStr;
    private String expiration_date ;//保质期(天)
    private String description;//产品描述
    private Integer count;//库存
    private String kind;//分类(用于对商品进行分类)
    private String sort;//种类(比分类更加详细的分类)
    private int owner;//店铺
    private String ownerName;//店铺名


我在添加数据时,在点击保存后,前端报500错误。

HTTP状态 500 - 内部服务器错误
Request processing failed; nested exception is java.lang.NullPointerException
例外情况

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:209)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
根本原因。

java.lang.NullPointerException
    com.lt.service.impl.OwnerServiceImpl.addProducts(OwnerServiceImpl.java:50)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base/java.lang.reflect.Method.invoke(Method.java:564)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    com.sun.proxy.$Proxy30.addProducts(Unknown Source)
    com.lt.controller.OwnerController.addProducts(OwnerController.java:65)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base/java.lang.reflect.Method.invoke(Method.java:564)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:209)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)

我自己对自己的问题有几个想法,由于对springsecurity不熟练,也是搬过来直接用的:
1.是不是因为springsecurity拦截了,但我有个owner.jsp和addProduts.do设置不被拦截:
<security:http pattern="/owner.jsp" security="none"/>
<security:http pattern="/owner/addProducts.do" security="none"/>

2.那就是我的程序编码的问题,但我实在找不到哪里的问题还请指点
3.我还有一个疑惑就是form里的数据顺序必须和bean里的顺序也是一样的吗

断点如图:

img

问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

希望得到解答,或者提供一些思路

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

相关推荐 更多相似问题