gpt4 book ai didi

spring - 使用 Hibernate 和 Spring MVC 的 JSP

转载 作者:行者123 更新时间:2023-11-28 23:16:19 25 4
gpt4 key购买 nike

美好的一天!当我将 war 部署到 tomcat 时,它会给我这个异常:可能有人可以使用@Transactional 注释帮助解决该问题,但在 2 周内无法解决该问题:(

    n22-Feb-2018 14:52:54.125 SEVERE [http-nio-8080-exec-11] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/page] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.mycompany.app.controller.TypeController.listAll(TypeController.java:24)
at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

web.xml

    <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

mvc-dispatcher-servlet.xml

    <context:component-scan base-package="com.mycompany.app" />
<context:component-scan base-package="com.mycompany.app.controller" />
<context:component-scan base-package="com.mycompany.app.service" />

<mvc:annotation-driven />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

TypeController.java

@Controller
public class TypeController {

private TypeServiceImpl typeService;

@RequestMapping(value = "/main")
public String listAll(Map<String, Object> map) {
map.put("type", new Type());
map.put("typeList", typeService.listAll());
return "index";
}

@RequestMapping("/")
public String home() {
return "redirect:/main";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("type") Type type, BindingResult bindingResult) {
typeService.add(type);
return "redirect:/main";
}

@RequestMapping(value = "/delete/{id}")
public String deleteType(@PathVariable("id") Integer id) {
typeService.delete(id);
return "redirect:/main";
}
}

TypeServiceImpl.java不要使用 DAO

@Service
public class TypeServiceImpl implements TypeService {

private SessionFactory sessionFactory;

@Override
@Transactional
public void add(Type type) {
sessionFactory.getCurrentSession().save(type);
}

@Override
@Transactional
@SuppressWarnings("unchecked")
public List<Type> listAll() {
return sessionFactory.getCurrentSession().createQuery("from Type").list();
}

@Override
@Transactional
public void delete(Integer id) {
Type type = sessionFactory.getCurrentSession().load(Type.class, id);
if (type != null) {
sessionFactory.getCurrentSession().delete(type);
}
}
}

Type.java

@Entity
@Table(name = "type")
public class Type {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_type")
private Integer id_type;

@Column(name = "type_name")
private String type_name;

// Communication with Event
@OneToMany(fetch = FetchType.LAZY, mappedBy = "type")
private List<Event> event;

public Type() {
}

public Type(Integer id_type, String type_name) {
this.id_type = id_type;
this.type_name = type_name;
}
//Default getters and setters
}

index.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Page</title>
</head>
<body>
<form:form method="post" action="add" modelAttribute="type">
<table>
<tr>
<td><form:label path=id_type>Type's ID</form:label></td>
<td><form:input path="id_type" /></td>
</tr>
<tr>
<td><form:label path=type_name>Type name</form:label></td>
<td><form:input path="type_name" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
<h3>Types</h3>
<c:if test="${!empty typeList}">
<table class="data">
<tr>
<th>Type ID</th>
<th>Type Name</th>
<th>&nbsp;</th>
</tr>
<c:forEach items="${typeList}" var="type">
<tr>
<td>${type.id_type}}</td>
<td>${type.type_name}</td>
<td><a href="delete/${type.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>

感谢收看!!!

最佳答案

我认为您的服务可能缺少一个 @Autowired 注释。

@Autowired
private TypeService typeService;

目前我看不到你在哪里实例化服务类。

关于spring - 使用 Hibernate 和 Spring MVC 的 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48926887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com