1

我收到以下错误:-登录错误。原因:无法获取 JDBC 连接;嵌套异常是 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'dbname'当我尝试连接错误的数据库时。但我想处理该异常并想显示一些自定义消息。

这是我的配置文件:- Spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <!--<http pattern="/abc/**" security="none" /> -->

    <http auto-config="true" use-expressions="true">

        <intercept-url pattern="/admin*" access="hasRole('SYS_ADMIN')" />
        <intercept-url pattern="/cab-stop-admin*" access="hasAnyRole('SYS_ADMIN','CABSTOP_ADMIN')" />
        <intercept-url pattern="/driver*" access="hasAnyRole('DRIVER','SYS_ADMIN','CABSTOP_ADMIN')" />
        <intercept-url pattern="/customer*" access="hasAnyRole('CUSTOMER','SYS_ADMIN','CABSTOP_ADMIN')" />

        <form-login login-page="/login" default-target-url="/role-check"
            authentication-failure-url="/login?error=true" />
            <remember-me key="_spring_security_remember_me"/>
        <logout logout-success-url="/login" />

         </http> 

         <authentication-manager> 
        <authentication-provider> 
        <password-encoder hash="md5"  />
        <jdbc-user-service data-source-ref="fmsDataSource"  
        users-by-username-query="select Username,Password, 'true' as enabled from login_details where Username=?"
        authorities-by-username-query="select u.Username, ur.Role_Name from login_details u, role_master ur where u.Role_Master_Id = ur.Role_Master_Id and u.Username =? " />
    </authentication-provider>
    </authentication-manager>

</beans:beans>

登录.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<jsp:include page="header.jsp" />

<html>
<head>
<title><spring:message code="message.home" /></title>
</head>

<body>
<c:if test="${not empty param.error}">
    <font color="red"><br /> Login error. 
    Reason : ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} </font>

</c:if>
<form  name="loginForm" onsubmit="return validateForm();">
<h1><spring:message code="message.login" /></h1>
<table>
    <tr>
        <td align="right"><spring:message code="message.username"
            text="default text" /></td>
        <td><input type="text" name="j_username" id="j_username" autofocus="autofocus"/></td>
    </tr>
    <tr>
        <td align="right"><spring:message code="message.password"
            text="default text" /></td>
        <td><input type="password" name="j_password" /></td>
    </tr>

    <tr>
        <label  for='_spring_security_remember_me'>
                        Remember me:
                        <input type='checkbox' name='_spring_security_remember_me' value="on"/>
    </tr>

    <tr>
        <td colspan="2" align="right"><input type="submit"
            value="<spring:message code="message.login" />" /></td>
        <td colspan="2" align="right"><a href="<%=request.getContextPath()%>/customer/register"><spring:message
            code="message.register" text="default text" /></a></td>
    </tr>
    <tr>
        <td colspan="2" align="right"><a href="<%=request.getContextPath()%>/forgot-password"><spring:message
            code="message.forgotpassword" text="default text" /></a></td>
    </tr>
</table>
</form>
</body>
<jsp:include page="footer.jsp" />
</html>

休眠配置.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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


    <context:component-scan base-package="com.cabfms.dao"/>

  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="fmsDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${db.driverClassName}" p:url="${db.url}"
        p:username="${db.username}" p:password="${db.password}" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="fmsDataSource" />
        <property name="packagesToScan" value="com.cabfms.entities" />
        <property name="hibernateProperties">

            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
            <!--<props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
                <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
                <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
                <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
                <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
                <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
                <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
                <prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop>


            </props>


        --></property>
    </bean>

      <!--Transaction Manager Added -->  
    <bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name = "sessionFactory" ref = "sessionFactory" />
</bean>


</beans>

web.xml

<web-app>

    <display-name>Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/dispatcher-servlet.xml,
            /WEB-INF/hibernate-config.xml,
            /WEB-INF/spring-security.xml,
            /WEB-INF/application-context.xml
        </param-value>
    </context-param>



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

    <!-- Spring MVC -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <!--<url-pattern>/services/*</url-pattern> -->
        <!--<url-pattern>/views/*</url-pattern> -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping><!--

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

-->
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error-page</location>
</error-page>
</web-app>
4

1 回答 1

1

您可以让您的应用程序容器处理此类异常。

应用程序中未捕获的异常可以转发到部署描述符 ( web.xml) 中定义的错误页面。

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error</location>
</error-page>

您只需要输入您想要处理的异常类型和在应用程序错误时将用户转发到的位置。

请记住让匿名用户可以看到此错误页面,以防在成功验证之前发生错误。

<sec:intercept-url pattern="/error" access="permitAll"/>
于 2012-11-05T09:45:13.613 回答