spring-boot - 从jsp表单更新数据库表
问题描述
我有一个 jsp 视图,其中显示了一组参数(复选框和测试字段),用户可以修改并保存更改。参数存储在一个名为“PropertiesEnvironment”的实体中,该实体扩展了“Environment”实体。我能够正确显示参数,但我不知道如何在用户进行修改后保存更改。
我试过用 servlet 来做,但我总是得到空指针错误。
这是我的 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(Native Method) 时为 null ~[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.罐子: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] at org.springframework.web.servlet .mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.web.servlet.mvc.method。 AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.web.servlet.DispatcherServlet。doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] at 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] at 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] at 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] at 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] at 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] at 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] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~ [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE] at 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] at 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] at 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.org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun( NioEndpoint.java:1471) [tomcat-embed-core-8.5.32.jar:8.5.32] at 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] at 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]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] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181] at 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章)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] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181] at 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章)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]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]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]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]
关于如何更新数据库的任何想法?谢谢你的帮助!
解决方案
推荐阅读
- python-3.x - 如何从 Python 列表中打印对象的名称
- ruby - 是否可以让自定义库模块了解 Chef 中的食谱属性?
- powershell - 像这样验证代码签名是否安全?
- excel - 我的 VBA 多次运行宏。但只需要运行一次
- javascript - 如何在没有门户的 Azure Web 应用程序中更改启动命令?
- php - Symfony4:不同域的单独日志文件
- mysql - MySQL proc 表经常崩溃
- amazon-web-services - 将整个模式从一个红移集群复制到另一个
- javascript - 无法读取 null 的属性“已检查”
- flutter - 基于父级的浮动按钮触发 TabBar 中的函数