在 Spring Boot 中使用重定向 [英] Using Redirect in Spring Boot
问题描述
我在 Spring Boot 应用程序中使用重定向功能时遇到问题.如我下面的代码所示,我返回重定向:/aucConfirm/",但是当我启动它时,我收到此应用程序没有显式映射"错误.
@Controller@RequestMapping("/")公共类欢迎控制器{@自动连线AuctionItemRepository aucRepository;//通过 application.properties 注入@Value("${welcome.message:test}")private String message = "Hello World";@RequestMapping(value = "/")公共字符串欢迎(地图<字符串,对象>模型){model.put("message", this.message);返回欢迎";}@RequestMapping(value = "/sell", method = RequestMethod.GET)公共字符串 addAuction(模型模型,HttpServletRequest 请求){model.addAttribute("newAuction", new AuctionItem());return "NewAuction";}@RequestMapping(value = "/sell", method = RequestMethod.POST)public String saveAuction(@ModelAttribute AuctionItem newAuction, RedirectAttributes 属性){返回重定向:/aucConfirm";}@RequestMapping(value = "/aucConfirm", method = RequestMethod.GET)公共字符串确认新拍卖(模型模型,HttpServletRequest 请求){返回 "aucConfirm";}}
这是我当前的配置类 -
@SpringBootApplication@ComponentScan@EnableJpaRepositories("全部")@EntityScan("全部")公共类 AuctionWebApplication 扩展 SpringBootServletInitializer {@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(AuctionWebApplication.class);}public static void main(String[] args) 抛出异常 {SpringApplication.run(AuctionWebApplication.class, args);}@豆数据源数据源(){DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/auctiondb?zeroDateTimeBehavior=convertToNull");dataSource.setUsername("root");dataSource.setPassword("toor");返回数据源;}@豆public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();entityManager.setDataSource(dataSource);entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());entityManager.setPackagesToScan("All");属性 jpaProperties = new Properties();jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");jpaProperties.setProperty("hibernate.hbm2ddl.auto", "更新");jpaProperties.setProperty("hibernate.id.new_generator_mappings", "false");entityManager.setJpaProperties(jpaProperties);返回实体管理器;}
}
更新
它可能与我试图建立重新提交的jsp页面有关吗?我这里有一个 mvc 表单 -
当用户点击下一个按钮时,它应该点击redirect:/aucConfirm" -
<button type="submit" name="submit" class="btn btn-success" form="addNewAuc">Next</button>
这段代码有什么问题吗?在这一点上,我的想法不多了.我是否需要向我的 Spring Boot 配置类添加任何内容才能使其正常工作?- 请帮忙!
更新 2
当我在此表单中输入详细信息并选择下一步时,我希望它重定向到-
根本不重定向并保留链接 http://localhost:8080/AuctionWebsite/sell
http://localhost:8080/AuctionWebsite/aucConfirm 手动输入时有效
此时我有点绝望,非常感谢您的帮助.
使用此代码
redirect:aucConfirm
代替
redirect:/aucConfirm
根据您更新的问题.好像
<块引用>此应用程序没有显式映射"错误
用于/error url;
实际错误是
<块引用>对象auctionItem"的验证失败
所以问题不在于重定向,而是与验证相关的其他问题.您可能会在控制台日志中发现特定错误.
Im having trouble using redirect functionality within my spring boot application. As shown in my code below im returning "redirect:/aucConfirm/" but when i initiate this i get a "This application has no explicit mapping" error.
@Controller
@RequestMapping("/")
public class WelcomeController {
@Autowired
AuctionItemRepository aucRepository;
// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";
@RequestMapping(value = "/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
@RequestMapping(value = "/sell", method = RequestMethod.GET)
public String addAuction(Model model, HttpServletRequest request) {
model.addAttribute("newAuction", new AuctionItem());
return "NewAuction";
}
@RequestMapping(value = "/sell", method = RequestMethod.POST)
public String saveAuction(@ModelAttribute AuctionItem newAuction, RedirectAttributes attributes){
return "redirect:/aucConfirm";
}
@RequestMapping(value = "/aucConfirm", method = RequestMethod.GET)
public String confirmNewAuction(Model model, HttpServletRequest request) {
return "aucConfirm";
}
}
This is my current configuration class -
@SpringBootApplication
@ComponentScan
@EnableJpaRepositories("All")
@EntityScan("All")
public class AuctionWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AuctionWebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(AuctionWebApplication.class, args);
}
@Bean
DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/auctiondb?zeroDateTimeBehavior=convertToNull");
dataSource.setUsername("root");
dataSource.setPassword("toor");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setPackagesToScan("All");
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update");
jpaProperties.setProperty("hibernate.id.new_generator_mappings", "false");
entityManager.setJpaProperties(jpaProperties);
return entityManager;
}
}
Update
Could it possibly have something to do with my jsp page that im trying to establish the recommit? I have a mvc form here -
<mvc:form class="form-inline" action="${action}" method="post" modelAttribute="newAuction" id="addNewAuc">
and when the user hits the next button it should hit the "redirect:/aucConfirm" -
<button type="submit" name="submit" class="btn btn-success" form="addNewAuc">Next</button>
Is there anything wrong with this code? Im running out of ideas at this point. Do i need to add anything to my spring boot configuration class to get it working? - Please help!
Update 2
http://localhost:8080/AuctionWebsite/sell
When i enter details int this form and select next i want it to redirect to- http://localhost:8080/AuctionWebsite/aucConfirm
However this is what appears-
not redirecting at all and remaining with link http://localhost:8080/AuctionWebsite/sell
http://localhost:8080/AuctionWebsite/aucConfirm works when entered manually
Im getting a little bit desperate at this point, would really appreciate any help.
Use this code
redirect:aucConfirm
Instead of
redirect:/aucConfirm
As per your updated question. it seems that
"This application has no explicit mapping" error
Is for /error url;
The actual error is
Validation failed for object 'auctionItem'
So problem is not with redirection , it is something else related to validation. You may find specific error on console log.
这篇关于在 Spring Boot 中使用重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!