Struts 2和Hibernate登录表单

时间:2013-12-14 18:08:58

标签: java hibernate jsp struts2

我正在尝试使用struts和hibernate构建一个简单的登录表单。成功登录后,Index.jsp是欢迎文件,用户将被重定向到home.jsp。我正在使用一个客户拦截器,以便在没有登录的情况下直接访问home.jsp。我面临的问题是

  1. 运行应用程序

    时出现此异常

    org.apache.jasper.JasperException:找不到Struts调度程序。
    这通常是由于使用没有关联过滤器的Struts标记引起的。 Struts标记仅在请求通过其servlet过滤器时可用, 初始化此标记所需的Struts调度程序。 - [未知位置]

  2. 无需登录即可直接访问home.jsp。

  3. 以下是文件 的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
    
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    

    struts.xml中

    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
            <!-- Configuration for the default package. -->
            <package name="default" extends="hibernate-default">
                <Interceptors>
                    <Interceptor name="LoginInterceptor" class="com.riteshsangwan.ossoc.interceptors.LoginInterceptor" />
                </Interceptors>
    
                <action name="login" class="com.riteshsangwan.ossoc.action.LoginAction">
                    <result name="success" type="chain">wolcomeaction</result>
                    <result name="imput">/index.jsp</result>
                    <result name="error">/index.jsp</result>
                </action>
    
                <action name="welcomeaction">
                    <interceptor-ref name="LoginInterceptor"></interceptor-ref>
                    <interceptor-ref name="defaultStack"></interceptor-ref>
                    <result name="login">/index.jsp</result>
                    <result name="success" >/home.jsp</result>
    
                </action>
            </package>
    </struts>
    

    LoginInterceptor.java

    package com.riteshsangwan.ossoc.interceptors;
    
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.opensymphony.xwork2.Action;
    import java.util.Map;
    
    public class LoginInterceptor extends AbstractInterceptor{
    
        @Override
        public String intercept(ActionInvocation ai) throws Exception {
            Map<String, Object> session= ai.getInvocationContext().getSession();
            if(session.get("userObject")==null)
            {
                return Action.LOGIN;
            }
            else
            {
                return Action.SUCCESS;
            }
    
        }
    
    }
    

    LoginAction.java

    package com.riteshsangwan.ossoc.action;
    import com.opensymphony.xwork2.ActionSupport;
    import com.riteshsangwan.ossoc.entities.Users;
    import org.apache.commons.lang.StringUtils;
    import com.riteshsangwan.ossoc.business.UsersDAOImpl;
    import java.util.Map;
    import javax.servlet.http.HttpSession;
    import org.apache.struts2.interceptor.SessionAware;
    
    public class LoginAction extends ActionSupport implements SessionAware{
        private String email;
        private String password;
        private Users user;
        private Map<String, Object> userSession;
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String execute(){
            if(verify())
            {
                return ActionSupport.SUCCESS;
            }
    
            else
            {
                return ActionSupport.LOGIN;
            }
    
    }
    
        @Override
        public void validate(){
            if(StringUtils.isEmpty(getEmail()) || StringUtils.isEmpty(getPassword()))
            {
                addFieldError("Error","oops! something went wrong try again");
            }
        }
    
        private boolean verify(){
            String email=getEmail();
            String password=getPassword();
            UsersDAOImpl udl=new UsersDAOImpl();
            user = udl.LoginVerify(email, password);
            if(user!=null)
            {
                userSession.put("userObject", user);
                userSession.put("userName", user.getFname()+" "+user.getLname());
                userSession.put("email", user.getEmail());
                userSession.put("userId", user.getUid());
                return true;
            }
            else
            {
                return false;
            }
        }
    
        @Override
        public void setSession(Map<String, Object> map) {
            userSession=map;
        }
    
    
    }
    

    的index.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Login</title>
        </head>
        <body>
            <s:form action="login" method="POST">
                <fieldset>
                    <s:label>Email:</s:label>
                    <s:textfield name="email"></s:textfield>
                    <s:label>Password:</s:label>
                    <s:password name="password"></s:password>
                    <s:submit name="loginsubmit"></s:submit>
                </fieldset>
            </s:form>
        </body>
    </html>
    

    已编辑:pom.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.riteshsangwan</groupId>
        <artifactId>ossoc</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
    
        <name>ossoc</name>
    
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <type>jar</type>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.5</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.2.8.Final</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>4.2.8.Final</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate.common</groupId>
                <artifactId>hibernate-commons-annotations</artifactId>
                <version>4.0.4.Final</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-testing</artifactId>
                <version>4.2.8.Final</version>
            </dependency>
    
            <dependency>
                <groupId>com.jgeppert.struts2.jquery</groupId>
                <artifactId>struts2-jquery-plugin</artifactId>
                <version>3.6.1</version>
            </dependency>
    
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.3.15.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>2.3.15.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-junit-plugin</artifactId>
                <version>2.3.15.3</version>
            </dependency>
    
           <dependency>        
                <groupId>com.opensymphony</groupId>
                <artifactId>xwork-core</artifactId>
                <version>2.1.6</version>
              <type>jar</type>
           </dependency>    
    
           <dependency>        
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.27</version>
           </dependency>    
    
    
    
           <dependency>        
                <groupId>org.apache.struts.xwork</groupId>
                <artifactId>xwork-core</artifactId>
                <version>2.3.15.3</version>
           </dependency>                                
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <compilerArguments>
                            <endorseddirs>${endorsed.dir}</endorseddirs>
                        </compilerArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${endorsed.dir}</outputDirectory>
                                <silent>true</silent>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>javax</groupId>
                                        <artifactId>javaee-endorsed-api</artifactId>
                                        <version>7.0</version>
                                        <type>jar</type>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

1 个答案:

答案 0 :(得分:1)

过滤器映射有点不正确,正确的映射应该是:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

其次,拦截器声明有点故障(案例问题)。示例登录拦截器堆栈可能如下所示:

 <interceptors>
            <interceptor name="nlogin" class="interceptors.LoginInterceptor"/>
            <interceptor-stack name="loginStack">
                <interceptor-ref name="nlogin"/>
                <interceptor-ref name="store">
                    <param name="operationMode">AUTOMATIC</param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="loginStack"/>

default-interceptor-ref负责默认拦截器堆栈,该拦截器堆栈将针对该包中定义的所有操作执行。

第三,拦截器的代码总是执行&amp;返回结果,而不给其他拦截器,甚至是执行的动作。示例登录拦截器可能如下所示:

@Override
    public String intercept(ActionInvocation ai) throws Exception {
        try {
            if (ai.getInvocationContext().getSession().get("admin") == null) {
                Object action = ai.getAction();
                if (action instanceof ValidationAware) {
                    ((ValidationAware) action).addActionError("Unauthorized access. Please Login first");
                }
                return "login";
            }
            return ai.invoke();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ai.invoke();
    }
相关问题