【发布时间】:2015-09-02 15:08:46
【问题描述】:
我一直在从this website 构建一个 Spring webapp。当我运行教程代码时,应用程序运行完美。但是,当我运行修改后的代码版本时,出现以下错误:
我不知道为什么会这样;我有效地复制并粘贴了整个代码库,只是将数据更改为 Campaign 而不是 Strategy。
这是我的配置类:
package com.bridge.campaignspring.config;
import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScan("com.bridge.campaignspring")
@PropertySource("classpath:database.properties")
public class DatabaseConfig {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
@Resource
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
private Properties hibProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
return properties;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
sessionFactoryBean.setHibernateProperties(hibProperties());
return sessionFactoryBean;
}
}
我无法访问我创建的任何已创建的 .jsp。我只看到 404 错误。如果需要更多代码来解决此问题,请询问;我可以发布任何需要的东西。
编辑:这是我的 CampaignController 类:
package com.bridge.campaignspring.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.bridge.campaignspring.model.Campaign;
import com.bridge.campaignspring.service.CampaignService;
@Controller
@RequestMapping(value="/campaign")
public class CampaignController {
@Autowired
private CampaignService campaignService;
@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addCampaignPage() {
ModelAndView modelAndView = new ModelAndView("campaign-add");
modelAndView.addObject("campaign", new Campaign());
return modelAndView;
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView addingCampaign(@ModelAttribute Campaign campaign) {
ModelAndView modelAndView = new ModelAndView("home");
campaignService.addCampaign(campaign);
String message = "Campaign was added sucessfully";
modelAndView.addObject("message", message);
return modelAndView;
}
@RequestMapping(value="/list")
public ModelAndView listOfCampaigns() {
ModelAndView modelAndView = new ModelAndView("campaign-list");
List<Campaign> campaigns = campaignService.getCampaigns();
modelAndView.addObject("campaigns", campaigns);
return modelAndView;
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public ModelAndView editCampaignPage(@PathVariable Integer id) {
ModelAndView modelAndView = new ModelAndView("campaign-edit");
Campaign campaign = campaignService.getCampaign(id);
modelAndView.addObject("campaign", campaign);
return modelAndView;
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public ModelAndView editingCampaign(@ModelAttribute Campaign campaign, @PathVariable Integer id) {
ModelAndView modelAndView = new ModelAndView("home");
campaignService.updateCampaign(campaign);
String message = "Campaign edited successfully";
modelAndView.addObject("message", message);
return modelAndView;
}
@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public ModelAndView deleteCampaign(@PathVariable Integer id) {
ModelAndView modelAndView = new ModelAndView("home");
campaignService.deleteCampaign(id);
String message = "Campaign deleted successfully";
modelAndView.addObject("message", message);
return modelAndView;
}
}
【问题讨论】:
-
你能告诉我们你的
web.xml吗? -
按照教程,我没有 web.xml 文件。
-
你在博文中提到过控制器吗?
-
@lkrnac 是的,我有教程中概述的控制器。我将编辑 OP 以反映我的更改。
-
你从哪里得到
/CampaignSpringEx02?
标签: java spring hibernate spring-mvc tomcat