当前位置: 首页 > 知识库问答 >
问题:

Spring MVC自己呈现index.jsp

华谭三
2023-03-14

我正在尝试使用Spring MVC+Spring Security+Hibernate创建一个简单的应用程序。

我需要一个不同的URI用于2个不同的流,它们将像:- /candide/... /admin/...

  <display-name>spring</display-name>
  <welcome-file-list>
    <welcome-file>WEB-INF/pages/login.jsp</welcome-file>
  </welcome-file-list>
  <!-- Enables 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>

    <!-- End of Spring Security Configuration -->

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/spring-security.xml
    </param-value>
    </context-param>

</web-app>
     <context:annotation-config />
        <context:property-placeholder location="/config/appConfig.properties" />
        <context:component-scan base-package="com.ctlin">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>

        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.jdbcurl}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="${db.password}" />
            <property name="initialSize" value="${db.minPoolSize}" />
        </bean>

        <!-- Hibernate 4 SessionFactory Bean definition -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                <list>
                    <value>com.ctlin.bean.CandidateDetailsBean</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <!-- <prop key="hibernate.default_schema">${db.default_schema}</prop> -->
                    <prop key="hibernate.dialect">${db.dialect}</prop>
                    <prop key="hibernate.format_sql">${db.format_sql}</prop>
                    <prop key="hibernate.show_sql">${db.show_sql}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</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>

        <bean id="authenticateDao" class="com.ctlin.dao.impl.AuthenticateUserDAOImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <bean id="authenticateService" class="com.ctlin.service.impl.AuthenticateUserServiceImpl">
            <property name="authenticateUserDao" ref="authenticateDao" />
    </bean>
</beans>

mvc-dispatcher-servlet.xml

<context:annotation-config />
<context:component-scan base-package="com.ctlin.web.controller" />

<mvc:annotation-driven/>
<mvc:resources mapping="/**assets**" location="/assets/" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

spring-security.xml

<http auto-config="true" use-expressions="true">
    <!-- <access-denied-handler error-page="AccessDenied"/> -->

    <intercept-url pattern="/candidate**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**/assets/**" access="permitAll" />
    <intercept-url pattern="/**/login" access="permitAll" />
    <!-- <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /> -->
    <intercept-url pattern="/**" access="isFullyAuthenticated()" />
    <csrf/>
    <session-management invalid-session-url="/login" 
                        session-authentication-error-url="/login?sessionexpired"
                        session-fixation-protection="migrateSession">
        <concurrency-control error-if-maximum-exceeded="true" expired-url="/login" max-sessions="1"/>
    </session-management>
    <form-login login-page="/candidate/login" authentication-failure-url="/candidate/login?error" 
                username-parameter="userId" password-parameter="password" 
                default-target-url="/candidate/registerDetails"
                login-processing-url="/j_spring_security_check"/>
    <logout delete-cookies="JSESSIONID" logout-success-url="/candidate/login?logout" 
                invalidate-session="true" />
</http>

<authentication-manager>
  <authentication-provider>
    <user-service>
        <user name="test" password="test" authorities="ROLE_USER" />
    </user-service>
  </authentication-provider>
</authentication-manager>

控制器

@Controller
@RequestMapping("/candidate")
public class CandidateLoginController {

private static final Logger logger = LoggerFactory.getLogger(CandidateLoginController.class);

    private HttpSession httpSession;

    @Autowired(required=true)
    private IAuthenticateUserSvc authenticateService;

    public final void setAuthenticateService(IAuthenticateUserSvc authenticateService) {
        this.authenticateService = authenticateService;
    }



/**
     * @param null
     * @return the name of the login view
     */
    @RequestMapping(value = { "/", "/login" }, method = RequestMethod.GET)
    protected ModelAndView displayLoginPage(@RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "sessionexpired", required = false) String sessionExpired,
            @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView mav = new ModelAndView("login");
        logger.debug("Inside displayLoginPage method");

        if(error!=null) {

            mav.addObject("error", "Either username or password is incorrect");
        }
        else if(sessionExpired != null) {
            mav.addObject("error", "Your session has expired. Kindly login again.");
        }
        else if (logout != null) {

            mav.addObject("msg", "You've been logged out successfully.");
        }

        return mav;
    }

    @RequestMapping(value = "/registerDetails")
    public ModelAndView registerCandidateDetails() {

        System.out.println("candidate get method");
        ModelAndView model = new ModelAndView();
        model.setViewName("registerDetails");

        return model;

    }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page session="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Candidate Selection Tool</title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css" />
    <link rel="stylesheet" href="assets/css/normalize.css" />
    <link rel="stylesheet" href="assets/css/style.css" />
</head>

<body onload='document.loginForm.userId.focus();'>

    <div class="container">
        <!-- start of main body -->
        <div class="row page-header text-center">
            <h1>Candidate Selection Tool</h1>
        </div>

        <div class="col-md-6">
            <form name="loginForm" action="<c:url value='j_spring_security_check' />"
                class="form-horizontal" method="post" role="login">
                <fieldset class="col-md-9 scheduler-border">
                    <legend class="scheduler-border">Login</legend>
                    <c:if test="${not empty error}">
                        <div class="row">
                            <div class="col-md-10 col-md-offset-1">
                                <p class="bg-danger text-center lead">${error}</p>
                            </div>
                        </div>
                    </c:if>
                    <c:if test="${not empty msg}">
                        <div class="row">
                            <div class="col-md-10 col-md-offset-1">
                                <p class="bg-info text-center lead">${msg}</p>
                            </div>
                        </div>
                    </c:if>
                    <div class="form-group has-feedback">
                        <label for="cuid" class="col-md-3 control-label">User ID</label>
                        <div class="col-sm-9">
                            <input id="cuid" type="text"
                                placeholder="Please enter your user id" name="userId"
                                class="form-control" /> <i
                                class="form-control-feedback glyphicon glyphicon-user"></i>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label for="password" class="col-md-3 control-label">Password</label>
                        <div class="col-sm-9">
                            <input id="password" type="password"
                                placeholder="Please enter your password" name="password"
                                class="form-control" /> <i
                                class="form-control-feedback glyphicon glyphicon-lock"></i>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-4 col-md-2">
                            <button type="submit" class="btn btn-success" name="Login">Login</button>
                        </div>
                        <div class="col-md-offset-2 col-md-4">
                            <button type="reset" class="btn btn-default">Reset</button>
                        </div>
                    </div>
                    <input type="hidden" name="${_csrf.parameterName}"
                        value="${_csrf.token}" />
                </fieldset>
            </form>
        </div>
    </div>
    <!-- end of main body -->

    <script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
────webapp
    ├───assets
    │   ├───css
    │   ├───fonts
    │   ├───img
    │   │   └───background
    │   └───js
    ├───config
    └───WEB-INF
        └───pages
            └───error

然而,当我访问:HTTP://localhost:8282/spring/candidate/login时,在名为'mvc-dispatcher'的DispatcherServlet中找不到URI为[/spring/candidate/assets/css/normalize.css]的HTTP请求的映射,甚至在输入正确的凭据之后,它仍然保留在同一个URL上,类似于循环。

请帮我找出问题所在。

共有1个答案

韦衡
2023-03-14

尝试将servlet映射表单/更改为*

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>
 类似资料:
  • 当我执行这个生成的pdf填充主报表数据,而不是子报表数据,只有标题。

  • 本文向大家介绍JSP 中Servlet的自己实现,包括了JSP 中Servlet的自己实现的使用技巧和注意事项,需要的朋友参考一下 JSP 中Servlet的自己实现 一 步骤 1、继承HttpServlet 2、重写doGet()或者doPost()方法 3、在web.xml中注册Servlet  二 HttpServlet类继承图 三 实例 1、创建MyFirstServletDemo web

  • 本人一直很想自己造个 jQuery 的小库,第一是满足下自己,第二是去体验下 jQuery 内部的基情。

  • 我正在编写一组定制的PrimeFaces组件,使用PrimeFaces5.0,并在JBoss EAP6.2中运行。 null 2.2在myFaceStest.taglib.xml中,我定义了输入标记: 2.3在input.java(我的自定义组件)中,我执行以下操作: 2.4此组件的呈现器包含以下内容: 给定这种设置,为什么自定义组件不会呈现?我的jboss日志中没有任何内容,即使日志级别设置为d

  • 我已经声明了一个用于iOS的自定义渲染器(和Android-工作正常)。 自定义呈现器主要设置背景色和文本颜色。 设置文本颜色对启用和禁用状态都很好,但我在不同状态下设置按钮的背景颜色时遇到了麻烦。 我无法找到任何关于Xamarin自定义呈现器的文档,而且Xamarin的一个已知错误是,我无法在Visual Studio中获得任何适用于iOS类的智能感知,到目前为止,我已经使用了我能找到的关于该主

  • 我附上JSON格式。我还需要将值从一个组件传递到另一个组件。除了localstorage之外,还有其他功能可以缓存值吗?