gpt4 book ai didi

java - Struts 2 和 Hibernate 登录表单

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:29 25 4
gpt4 key购买 nike

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

  1. 我在运行应用程序时遇到此异常

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

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

以下是文件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>

最佳答案

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

<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 负责处理将为该包中定义的所有操作执行的默认拦截器堆栈。

第三,拦截器的代码总是执行并返回结果,而不给其他拦截器,甚至是要执行的 Action 。示例登录拦截器如下所示:

@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();
}

关于java - Struts 2 和 Hibernate 登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586565/

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