我目前有两个问题,但先解释一下情况:
我有一个 Struts2 Hibernate 项目,然后我创建了一个类名 class CreateUserView(generics.CreateAPIView):
permission_classes = [permissions.AllowAny]
def create(self, request, *args, **kwargs):
serializer = CreateUserSerializer(data = request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
的拦截器堆栈。每次用户调用操作时,我都会调用该拦截器堆栈。
现在我的问题:
我的代码:
HibernateSessionInterceptor:
HibernateSessionInterceptor
课程控制器:
public class HibernateSessionInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = -4986134131287730428L;
/**
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#init()
*/
@Override
public void init() {
DAOFactory.setTheFactory(DAOFactories.HIBERNATE.getTheFactory());
super.init();
}
/**
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#destroy()
*/
@Override
public void destroy() {
DAOFactory.setTheFactory(null);
super.destroy();
}
/**
* @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// if(HibernateSessionManager.getSessionFactory().getCurrentSession().isOpen()) {
// HibernateSessionManager.getSessionFactory().getCurrentSession().close();
// }
HibernateSessionManager.getSessionFactory().getCurrentSession().beginTransaction();
return Action.SUCCESS;
}
}
struts.xml(资源文件夹):
public class CourseController {
private static final long serialVersionUID = 1L;
public String courseCode;
public String schoolName;
public String schoolNameWithoutArticle;
public String coursePrice;
public String secondCoursePrice;
public String email;
public int maximumRegistrations;
public int currentRegistrations;
public String execute() throws Exception {
System.out.println(schoolName);
DAOFactory.getTheFactory().getCourseDAO().findById("ADNM10005").delete();
return "success";
}
// NOT INCLUDED GETTERS AND SETTERS
}
web.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"
/> -->
<constant name="struts.multipart.maxSize" value="30000000" />
<!-- <constant name="struts.action.extension" value=",,"></constant> -->
<package name="houseoftyping" extends="struts-default">
<!-- strict-method-invocation="true"> -->
<!--*******************************************************************Interceptors********************************************************* -->
<interceptors>
<interceptor name="HibernateSessionInterceptor"
class="com.houseoftyping.controller.interceptor.HibernateSessionInterceptor"></interceptor>
<interceptor-stack name="HibernateStack">
<interceptor-ref name="HibernateSessionInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- **********************************************************************Actions********************************************************* -->
<default-action-ref name="UnderConstruction"></default-action-ref>
<action name="index">
<result>/index.jsp</result>
</action>
</package>
<!-- ********************** Remove comments of your xml file if it is without
errors. ************************ -->
<!-- ********************** Add new xml-files if needed. **************************************************** -->
<include file="struts/struts-dashboard.xml"></include>
</struts>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>HouseofTyping</display-name>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>,</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>401</error-code>
<location>/view/errorpages/ErrorPage401.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/view/errorpages/ErrorPage403.jsp</location>
</error-page>
</web-app>