我正在尝试为大学的一个项目和几个学生一起建立一个Springmvc应用程序,这是一本基于网络的烹饪书。
虽然Hello World工作正常,但我们使用hibernate存储数据的代码出现了一些困难的异常。
域类:
/**
* POJO which represents a Recipe.
* @author Nils Sommer
*
*/
@Entity
@Table(name="recipes")
public class Recipe implements Serializable {
private static final long serialVersionUID = 3239162951065313443L; // generated by eclipse
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="title")
private String title;
@Column(name="description")
private String description;
@Column(name="content")
private String content;
@Column(name="preparation_endurance")
private int preparationEndurance;
@Column(name="total_endurance")
private int totalEndurance;
@Column(name="creation")
private Date creation;
private List<Image> images;
private List<Rating> ratings;
// ----- Getters and setters here: -----
}
服务类:
@Service("recipeService")
@Transactional
public class RecipeService {
protected static Logger logger = Logger.getLogger("service");
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
public Recipe getRecipe(int id) {
logger.debug("Retrieving person with id: " + id);
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Get recipe
Recipe recipe = (Recipe) session.get(Recipe.class, id);
// Retrieve recipe
return recipe;
}
public List<Recipe> getAll() {
logger.debug("Retrieving all persons");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Create a Hibernate query (HQL)
Query query = session.createQuery("FROM Person");
// Retrieve all
return query.list();
}
public void add(String title, String description, String content,
int preparationEndurance, int totalEndurance, Date creation) {
logger.debug("Adding new person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Create a new Recipe
Recipe recipe = new Recipe();
recipe.setTitle(title);
recipe.setDescription(description);
recipe.setContent(content);
recipe.setPreparationEndurance(preparationEndurance);
recipe.setTotalEndurance(totalEndurance);
recipe.setCreation(creation);
// Save
session.save(recipe);
}
public void delete(Integer id) {
logger.debug("Deleting existing person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing recipe first
Recipe person = (Recipe) session.get(Recipe.class, id);
// Delete
session.delete(person);
}
public void edit(Integer id, String title, String description, String content,
int preparationEndurance, int totalEndurance, Date creation) {
logger.debug("Editing existing person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing recipe via id
Recipe recipe = (Recipe) session.get(Recipe.class, id);
// Assign updated values to this recipe
recipe.setTitle(title);
recipe.setDescription(description);
recipe.setContent(content);
recipe.setPreparationEndurance(preparationEndurance);
recipe.setTotalEndurance(totalEndurance);
recipe.setCreation(creation);
// Save updates
session.save(recipe);
}
}
控制器类:
package org.cookbookgeeks.webkochbuch.web;
/**
* @author Nils Sommer
*
* This is the Controller which processes all recipe requests.
*/
@Controller
public class RecipeController {
private static final Logger logger = LoggerFactory.getLogger(RecipeController.class);
@Resource(name="recipeService")
private RecipeService recipeService;
/**
* Shows a recipe.
* @param id of the recipe which wil be shown.
* @return the view recipe.jsp
*/
@RequestMapping(method=RequestMethod.GET, value="/recipe/{id}")
public String showRecipe(@PathVariable("id") int id, Model model) {
logger.debug("Returning view recipe with recipe.id=" + id);
// Get recipe.
Recipe recipe = recipeService.getRecipe(id);
model.addAttribute("recipe", recipe);
return "recipe";
}
/**
* Adds a recipe and shows it afterwards.
* @param recipe which is added.
* @return the view recipe with the id of the created recipe.
*/
@RequestMapping(method=RequestMethod.POST, value="/recipe/add")
public String addRecipe(@ModelAttribute("recipe") Recipe recipe) {
//TODO: create recipe from post parameter
return "/recipe/" + recipe.getId();
}
/**
* Deletes a recipe and redirects to the start page.
* @param id of the recipe which gets deleted.
* @return the view of the start page.
*/
@RequestMapping(method=RequestMethod.GET, value="/recipe/delete/{id}")
public String deleteRecipe(@PathVariable("id") int id) {
//TODO: delete recipe.
return "/";
}
/**
* Edits a recipe and show it afterwards.
* @param recipe which gets edited.
* @return the view recipe with the id of the recipe.
*/
@RequestMapping(method=RequestMethod.POST, value="/recipe/edit")
public String editRecipe(@ModelAttribute("recipe") Recipe recipe) {
//TODO: edit recipe.
return "/recipe/" + recipe.getId();
}
}
我们使用基于eclipse的Spring工具包sdk并在启动过程中获得以下异常:
HTTP Status 500 - Servlet.init() for servlet appServlet threw exception
type Exception report
message Servlet.init() for servlet appServlet threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet appServlet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)
root cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recipeController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'recipeService' is defined
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)
root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'recipeService' is defined
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:270)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:744)
note The full stack trace of the root cause is available in the VMware vFabric tc Runtime 2.9.3.RELEASE/7.0.42.A.RELEASE logs.
VMware vFabric tc Runtime 2.9.3.Release/7.0.42.A.Release
似乎在控制器类中的配方服务存在问题...
编辑:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SRC/main/Resources/META-INF/app-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.cookbookgeeks.webkochbuch" />
</beans>
src/main/WEB app/we b-INF/application context . XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="org.cookbookgeeks.webkochbuch" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<import resource="hibernate-context.xml" />
</beans>
src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="org.cookbookgeeks.webkochbuch.web" />
</beans:beans>
您是否在使用
问题是你有很多Spring上下文文件,你没有加载正确的文件。您有一个根上下文.xml
从Web定义和加载.xml
(我建议不要这样做,即使使用XML配置;如果必须,请使用从特定上下文XML导入
)。然后,对于您的 servlet,您正在加载 servlet 上下文.xml
,它只是扫描您的 Web
包的组件。您需要在Web中指向Spring的上下文.xml
来扫描基本包;这些应用程序上下文.xml
和应用程序上下文.xml
文件实际上并没有在任何地方加载。
我现在的建议是尽可能避免XML配置,而支持JavaConfig系统;我的整个应用程序只有一个主入口点@Configuration
类,然后组件会扫描任何其他@Configuration
s。
没有安装 San-CLI 需要安装 npm i -g san-cli 快速创建 san init <app-name> 创建的是 san 项目。 app-name 是要创建的工程项目目录,可以为.(即在当前目录下创建)。 指定脚手架创建 san init <template> <app-name> template 是工程项目脚手架地址,支持 github、icode、gitlab 等 re
项目初始化 俗话说磨刀不误砍柴工,在开始之前要完成一些准备工作以便移植其他框架的组件,首先我们得把框架给安装好,easySwoole是一个非常易于使用的框架,安装一样很简单,只需要切换到项目根目录 //命令行快速安装 bash <(curl https://www.easyswoole.com/installer.sh) //OR curl https://www.easyswoole.com/i
当云联壹云的First Node部署成功后,用户可根据使用场景快速引导配置云联壹云平台。 设置管理员用户 当First Node节点部署完成后,用户在浏览器中输入First Node节点的IP地址,如提示“您的连接不是私密连接”,请单击 “高级” 按钮,并单击“继续前往x.x.x.x(不安全)”,打开云管平台控制台。 在管理员注册页面,设置管理员账号、密码,单击 “注册” 按钮,创建管理员用户。
首先,我们新建一个目录 myblog,在该目录下运行 npm init 生成一个 package.json,如下所示: 注意:括号里的是默认值,如果使用默认值则直接回车即可,否则输入自定义内容后回车。 然后安装 express 并写入 package.json: npm i express@4.14.0 --save 新建 index.js,添加如下代码: const express = requ
首先,我们新建一个目录 myblog,在该目录下运行 npm init 生成一个 package.json,如下所示: 注意:括号里的是默认值,如果使用默认值则直接回车即可,否则输入自定义内容后回车。 然后安装 express 并写入 package.json: npm i express@4.14.0 --save 新建 index.js,添加如下代码: var express = requi
当我从静态项目目录运行firebase init时,项目设置失败,出现以下错误: 错误中的项目名称与firebaseConfig脚本中的项目ID不同。在我的firebase仪表板或项目详细信息中,没有列为项目名称的项目。我尝试过用--reauth注销和登录,并添加了一个项目别名,但错误是一样的,名称的差异也是一样的。让登录浏览器选项卡保持打开状态肯定不起作用。 项目名称似乎出了问题。我该怎么处理?