从jsp表单更新数据库表

时间:2019-06-26 08:58:27

标签: spring-boot jsp spring-data-jpa spring-data spring-repositories

我有一个jsp视图,其中显示了一组用户可以修改和保存更改的参数(复选框和测试字段)。 这些参数存储在扩展“环境”实体的名为“ PropertiesEnvironment”的实体中。 我能够正确显示参数,但是当用户进行修改后,我无法确定如何保存更改。

我尝试使用servlet来执行此操作,但是我总是收到Null指针错误。

这是我的PropertiesEnvironment实体:

@Entity
public class PropertiesEnvironment extends Environment implements Serializable {
    private PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();

    public PropertiesEnvironment() {
    }

    private Long lastPurgeTimestamp;

    @Column
    public Long getLastPurgeTimestamp() {
        return lastPurgeTimestamp;
    }

    public void setLastPurgeTimestamp(Long lastPurgeTimestamp) {
        this.lastPurgeTimestamp = lastPurgeTimestamp;
    }

  @Column(length=500000)
  @Lob
  @Type(type = "serializable")
  public Map<String, Serializable> getProperties() {
        return propertiesConfiguration.getProperties();
    }
    public void setProperties(Map<String, Serializable> properties) {
        propertiesConfiguration.setProperties(properties);
    }
    public int getIntValue(String tag, String defaultValue) {
        return propertiesConfiguration.getIntValue(tag, defaultValue);
    }
    public int getIntValue(String tag) {
        return propertiesConfiguration.getIntValue(tag);
    }
    public boolean getBooleanValue(String property, String defaultValue) {
        return propertiesConfiguration.getBooleanValue(property, defaultValue);
    }
    public boolean getBooleanValue(String tag) {
        return propertiesConfiguration.getBooleanValue(tag);
    }
    public void setBooleanValue(String tag, boolean value) {
        propertiesConfiguration.setBooleanValue(tag, value);
    }
    public String getStringValue(String tag) {
        String v = propertiesConfiguration.getStringValue(tag);
        return v == null ? "" : v;
    }
    public String getStringValue(String tag, String value) {
        return propertiesConfiguration.getStringValue(tag, value == null ? "" : value);
    }
    public void setStringValue(String tag, String value) {
        propertiesConfiguration.setStringValue(tag, value);
    }
    public void setIntValue(String tag, int value) {
        propertiesConfiguration.setIntValue(tag, value);
    }

这是我的环境实体

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name = "environment")
public class Environment implements Serializable, Cloneable {

    private static final long serialVersionUID = -6347094896871928639L;

    private long id;

    @ManyToOne
    private String code;

    private String name;

    private Set<String> terminals = new TreeSet<String>();

    @Id//(generate = GeneratorType.AUTO)
    @GeneratedValue
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(length = 30)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(length = 50)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

我的控制器:

    @Controller
public class PickingParametersController  {
    @Autowired
    private EnvironmentRepository environmentRepository;
    private PickingParametersRepository pickingParametersRepository;
    private EnvironmentPropertiesService environmentPropertiesService;

    public PickingParametersController(EnvironmentRepository environmentRepository , PickingParametersRepository pickingParametersRepository) {
        this.environmentRepository = environmentRepository;
        this.pickingParametersRepository = pickingParametersRepository;
    }

    @RequestMapping(value = {"/picking"}, method = RequestMethod.POST)
    public String select(HttpServletRequest request , Model model2) throws ServletException, IOException {
        String selected = request.getParameter("dropDownList");// get param from kendo dropdownlist

        List<PropertiesEnvironment> environmentList = pickingParametersRepository.findByCode(selected); // load params by id
        model2.addAttribute("properties",environmentList);
        return "picking";
    }
@RequestMapping(value = "success", method = RequestMethod.POST)
public String save(@ModelAttribute("propertiesEnvironment") PropertiesEnvironment propertiesEnvironment) {
    environmentPropertiesService.update(propertiesEnvironment);
    return "success";
}

    @RequestMapping(value = {"/picking"}, method = RequestMethod.GET)
    public String page(Model model) {
        List<PropertiesEnvironment> codeList = (List<PropertiesEnvironment>) environmentRepository.findAll();
        model.addAttribute("code",codeList);
        return "picking";
    }
}

我的存储库界面

@Repository
public interface PickingParametersRepository extends CrudRepository<PropertiesEnvironment, String> {
    @Query("SELECT E.properties FROM Environment E WHERE E.code = :code")
    List<PropertiesEnvironment> findByCode (@Param("code") String code);

环境属性服务

public interface EnvironmentPropertiesService {

public  PropertiesEnvironment update(PropertiesEnvironment propertiesEnvironment);

EnvironmentPropertiesServiceImpl

    @Transactional
@Service("environmentPropertiesService")
public class EnvironmentPropertiesServiceImpl implements EnvironmentPropertiesService{

    @Autowired
    private PickingParametersRepository pickingParametersRepository;

    @Override
    public PropertiesEnvironment update (PropertiesEnvironment propertiesEnvironment){
        return pickingParametersRepository.save(propertiesEnvironment);
    }

最后是我的JSP页面中的代码行:

<form method="post" modelAttribute="pickingparam" action="${pageContext.request.contextPath }/success">

                                             <c:forEach items="${properties}" var="p">

                                                 ID <input type="text" value="${p.id}"><br>
                                                 Channel <input type="text" value="${p.channel}"><br><br><br>
                                                 Total Lines Assignement    <input type="checkbox" name="totalLinesAssignment" value="1" id="id1"
                                                 <c:if test="${p.totalLinesAssignment == 'true'}">checked="checked"</c:if>/><br>
                                                 Total Lines Support    <input type="checkbox" name="totalLinesSupport" value="1" id="id10"
                                                 <c:if test="${p.totalLinesSupport == 'true'}">checked="checked"</c:if>/><br>
                                                 Total Pick Units Assignement   <input type="checkbox" name="totalPickUnitsAssignment" value="2" id="id2"
                                                 <c:if test="${p.totalPickUnitsAssignment == 'true'}">checked="checked"</c:if>/><br>
                                                 Total Pick Units Support   <input type="checkbox" name="totalPickUnitsSupport" value="3" id="id3"
                                                 <c:if test="${p.totalPickUnitsSupport == 'true'}">checked="checked"</c:if>/><br>
                                                 Inventory Pick Location Activity Type  <input type="text" value="${p.inventoryPickLocationActivityType}"><br>
                                                 Inventory Stock Location Activity Type  <input type="text" value="${p.inventoryStockLocationActivityType}"><br>
                                                 Inventory Last Digits EAN  <input type="text" value="${p.inventoryLastDigitsEAN}"><br>
                                                 Inventory Last Digits SSCC <input type="text" value="${p.inventoryLastDigitsSSCC}"><br>

                                            <input type="submit" value="Save"/>



                                            </c:forEach>

                                           </form>

这是堆栈跟踪:

  

java.lang.NullPointerException:空       在com.vocognition.ui.web.controller.PickingParametersController.save(PickingParametersController.java:62)〜[classes /:na]       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_181]       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_181]       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_181]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_181]       在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)〜[tomcat-embed-websocket-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)〜[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)上[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:800)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1471)[tomcat-embed-core-8.5.32.jar:8.5.32]       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)上[tomcat-embed-core-8.5.32.jar:8.5.32]       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_181]       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_181]       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.5.32.jar:8.5.32]       在java.lang.Thread.run(Thread.java:748)[na:1.8.0_181]

请问我如何进行数据库更新? 感谢您的帮助!

0 个答案:

没有答案