【问题标题】:HTTP STATUS 404: The requested resource is not availableHTTP STATUS 404:请求的资源不可用
【发布时间】:2014-10-01 09:27:15
【问题描述】:

当我启动 login.jsp 表单并输入用户名和密码时出现此错误 错误是 HTTP STATUS 404: 请求的资源不可用。

我错过了什么?有什么帮助吗?

LoginAction.java

package com.tutorialspoint.struts2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

 private String user;
 private String password;
 private String name;

public String execute() {
  String ret = ERROR;
  Connection conn = null;

  try {
     String URL = "jdbc:mysql://localhost/struts_tutorial";
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(URL, "root", "");
     String sql = "SELECT name FROM login WHERE";
     sql+=" user = ? AND password = ?";
     PreparedStatement ps = conn.prepareStatement(sql);
     ps.setString(1, user);
     ps.setString(2, password);
     ResultSet rs = ps.executeQuery();

     while (rs.next()) {
        name = rs.getString(1);
        ret = SUCCESS;
     }
  } catch (Exception e) {
     ret = ERROR;
  } finally {
     if (conn != null) {
        try {
           conn.close();
        } catch (Exception e) {
        }
     }
  }
  return ret;
 }

public String getUser() {
  return user;
 }

public void setUser(String user) {
  this.user = user;
 }

public String getPassword() {
  return password;
 }

public void setPassword(String password) {
  this.password = password;
 }

public String getName() {
  return name;
 }

public void setName(String name) {
  this.name = name;
 }
}

登录.jsp

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Login Example</title>
 </head>
    <body>
    <html:form action="/login" focus="userName">
      Username : <html:text property="userName" />
    <br>
      Password : <html:password property="password" />
    <br>
     <html:submit value="login" />
    </html:form>

    </body>
</html>

Web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>

Struts 配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <constant name="struts.devMode" value="true" />
  <package name="helloworld" extends="struts-default">

    <action name="loginaction" 
      class="com.tutorialspoint.struts2.LoginAction"
      method="execute">
     <result name="success">/success.jsp</result>
     <result name="error">/error.jsp</result>
    </action>

  </package>

 </struts>

这是我尝试过的 http://www.tutorialspoint.com/struts_2/struts_database_access.htm

【问题讨论】:

标签: java sql string jsp struts2


【解决方案1】:

So many questions 这些天来,tutorialspoint 中没有工作示例:(

那些教程是旧的,很多东西都变了。

  1. 更换旧的FilterDispatcher to the new StrutsPrepareAndExecuteFilter;
  2. 在您的项目中包含commons-lang3-3.x.jar
  3. 根据 a) 你的 servlet-api-x.x.jar 和 b) 你的 Servlet 容器功能更改你的 Servlet API 的版本(例如,如果你想使用它们,你需要确保你的应用服务器支持 Servlet API 3.0...记住不要在你的 WAR / EAR 中包含 servelt-api jar):你有一个混合配置,同时指定 2.5 和 3.0

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    

    fix that;

  4. 确保您拥有 all the required Struts2 libraries,所有这些都与最新版本(当前为 2.3.16.3)保持一致,以及它们的所有依赖项。

那么它应该可以工作。

之后,您可以开始寻找最佳实践,例如使用&lt;s:(Struts2 的默认前缀)而不是&lt;html:(Struts1 的默认前缀),以及使用connecting to a database from a business/DAO layer 而不是来自控制器(Action)。

【讨论】:

  • 感谢您的帮助。但问题是一样的。我认为数据库有问题,当我添加用户名、密码并单击登录按钮时,它显示“请求的资源不可用”和一些会话 ID。有什么办法可以解决这个问题吗?
  • 您并没有告诉我们您在此处建议的众多步骤中采取了哪些步骤。你换过过滤器吗?您是否更改了网络应用声明?您是否检查并最终对齐了所有 JAR?
  • 是的,你说的我都做了。但问题是一样的。有什么想法吗?
  • 您的 JSP 中是否有 &lt;%@ taglib prefix="html" uri="/WEB-INF/struts-tags.tld"%&gt;(作为第一行)?
  • 没错。但是然后在你的jsp中,将&lt;html: all 更改为&lt;s:,例如将&lt;html:form action="/login" focus="userName"&gt; 更改为`
    `
猜你喜欢
  • 1970-01-01
  • 2017-03-23
  • 2012-01-21
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2015-05-26
  • 2018-04-13
相关资源
最近更新 更多