java - 使用 JAVA 的 struts2 中的简单登录 Web 应用程序的第二页未加载?

标签 java xml jsp web-applications struts2

我创建了以下代码。但是在 apache tomcatv6.0 服务器中运行时,第二页即 Welcome.jsp 未加载,而第一页即 Login.jsp 已成功加载。有人能说编码有什么问题吗? 我已经在 J​​ava Resources 文件夹内的 src 文件夹内创建了 struts.xml。我正在使用 eclipse IDE。

StrutsDemo.java

package com.vishnu.struts;

import com.opensymphony.xwork2.ActionSupport;

public class StrutsDemo extends ActionSupport{
    String username;
    String password;
    String returnString=null;

    public String execute()
    {
        if(getUsername().equals("abc")&&getPassword().equals("abc123"))
        {

            returnString=SUCCESS;

        }
        else {
            returnString=ERROR;
        }
        return returnString;


    }

    public String getUsername() {
           return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

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

}  

登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Still TRY TRY...</title>
</head>
<body>

<s:textfield name="username"/>
<s:password name="password" value="Password"/>
<s:submit method="execute" label="Click" action="doLogin"/>


</body>
</html>

欢迎.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Thank God...</title>
</head>
<body>
<h1>Gud to see you!!!</h1>

</body>
</html>

struts.xml

<?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>

    <package name="default" namespace="/" extends="struts-default">


        <action name="doLogin" class="com.vishnu.struts.StrutsDemo" method="execute">

            <result name="success">/Welcome.jsp</result>
            <result name="error">/Login.jsp</result>
        </action>
        </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>STRUTS INTRO-VISHNU</display-name>

    <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>Login.jsp</welcome-file>

最佳答案

试试这个。你没有表格,那么你的字段是如何发布的..

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Still TRY TRY...</title>
</head>
<body>
<s:form action="doLogin">
  <s:textfield name="username"/>
  <s:password name="password" value="Password"/>
  <s:submit label="Click"/>
</s:form>

</body>
</html>

关于java - 使用 JAVA 的 struts2 中的简单登录 Web 应用程序的第二页未加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927267/

相关文章:

java - 将字符串写入文件

java - 在 Itext 中使用 XMLWorkerHelper 时未设置图像宽度和高度

javascript - 如何在 SAPUI5 的 Xml View 中编写无限行重复器

c# - 索引 XML 文件的问题

java - 如何将数据从一个jsp表单传输到另一个jsp表单

javascript - 如何从 iframe 打开弹出窗口

java - 添加到 ModelAndView 的变量似乎消失了

java - 添加 spring 组件列表覆盖相同类型的组件

java - 媒体数据库中的一对多过滤

java - 使用现有的 ant build.xml 文件创建一个 eclipse 项目