gpt4 book ai didi

java - eclipse Spring MVC 4 +Hibernate 和 MySQL + Tiles.xml 中的 HTTP 状态 404

转载 作者:行者123 更新时间:2023-11-29 18:33:15 25 4
gpt4 key购买 nike

我已经搜索了我的错误,但一无所获。我的代码中仍然存在错误,所以我必须在这里发布我的问题。当我在 eclipse 中使用 tomcat 服务器运行我的项目时,出现错误。我已经在 Spring MVC 4 + Hibernate 和 MongoDB 中完成了该项目。现在我必须使用 MySQL 切换到 Hibernate,我正在 eclipse 中启动该项目。但是当我用 hibernate 完成 spring 的配置时,它显示错误。错误和项目结构如下: enter image description here

web.xml

<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>ApplicationContext</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>DMS_MySQL</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DMS_MySQL</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/assets/*</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/pages/error/error404.jsp</location>
</error-page>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

</web-app>

SpringMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" //urls//">
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>

<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
<property name="order" value="2"></property>
</bean>
<bean id="jstlviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="3"></property>
</bean>
</beans>

applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Enable autowire -->
<context:annotation-config />
<context:component-scan base-package="com.cs" />

<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dms" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<!-- Session Factory Declaration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.cs.bean" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.default_schema">test</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<import resource="/WEB-INF/SpringMVC-servlet.xml"/>
</beans>

LoginController.java

包com.cs.controller;

public class LoginController {
@Autowired
private UserService userService;
@RequestMapping(value="/login")
public String execute(HttpSession session, Model model) throws Exception {
model.addAttribute("user", new UserBean());
UserBean tempUser = (UserBean) session.getAttribute("USER");
if (tempUser != null) {
return "redirect:/home";
}
return "logn";
}

@RequestMapping(value="/performLogin", method = RequestMethod.POST)
public String loginProcess(HttpSession session, Model model, @ModelAttribute("user") UserBean user,
BindingResult result) throws Exception
{
UserBean tempUser = (UserBean) session.getAttribute("USER");
if(tempUser != null)
{
return "redirect:/home";
}
if(user == null)
{
model.addAttribute("user", new UserBean());
return "login";
}
if(StringUtils.isBlank(user.getName()))
{
result.rejectValue("name", "error.required.username");
}
if(StringUtils.isBlank(user.getPassword()))
{
result.rejectValue("password", "error.required.password");
}
if(!result.hasErrors())
{
tempUser = userService.findByCredential(user);
if(tempUser != null)
{
if (tempUser.getRole().equals(RoleEnum.ADMIN)) {
session.setAttribute("USER", tempUser);
return "redirect:/home";
} else if (tempUser.getRole().equals(RoleEnum.CASHIER)) {
session.setAttribute("USER", tempUser);
return "redirect:/cHome";
}
}
else
{
result.reject("error.valid.usernamePassword");
}
}
user.setMobile("");
model.addAttribute("user", user);
return "login";
}
}

提前谢谢您。

最佳答案

尝试http://localhost:8080/DMS_MySQL/login 。另请尝试将您的登录 Controller 更改为:

@RequestMapping(value = {"/login", "/"}, method = RequestMethod.GET)
public String execute(HttpSession session, Model model) throws Exception {
model.addAttribute("user", new UserBean());
UserBean tempUser = (UserBean) session.getAttribute("USER");
if (tempUser != null) {
return "redirect:/home";
}
return "login";//typo in your code
}

我假设您已经在图 block 配置中定义了登录映射。

关于java - eclipse Spring MVC 4 +Hibernate 和 MySQL + Tiles.xml 中的 HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45519696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com