0
我在Spring應用程序中只有基於Java的配置。我創建了用戶實體和Dao來查找/創建用戶。 控制器有兩個GET方法 - 一個用於用戶,一個用於用戶列表和一個POST方法 - 用於創建/編輯用戶。JspViewResolver無法在基於java配置的spring應用程序中工作
在Tomcat 7服務器上運行後,它提供了404錯誤和消息'請求的資源不可用。
調度的servlet被配置在SpringWebAppInitializer.class:
public class SpringWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
servletContext.addListener(new ContextLoaderListener(appContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherServlet.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
視圖解析在ApplicationContextConfig.class定義:
@Configuration
@ComponentScan("pl.wybornie.entity")
@EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//other configuration beans...
}
的用戶列表和用戶窗體JSP文件下的WebContent項目的文件夾:/ WEB-INF/pages/user/*。jsp
負責創建視圖的控制器粘貼如下:
@Controller
@SessionAttributes({"user"})
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/user_list.html", method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request) {
model.addAttribute("userList", userDao.usersList());
return "user/userList";
}
@RequestMapping(value = "/create.html", method = RequestMethod.GET)
public String edit(@RequestParam(value="id", required=false) Long id, Model model) {
User user = userDao.findOrCreate(id);
model.addAttribute("user", user);
return "user/userForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute("user") User user, BindingResult bindingResult, HttpServletRequest request, Model model) {
userDao.saveOrUpdate(user);
return "redirect:user_list.html";
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
我forgetted這件事,但是很遺憾,這不是我的問題的原因... – argh 2015-04-04 13:37:54