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

时间:2013-11-12 10:54:49

标签: java xml jsp web-applications struts2

我创建了以下代码。但是在apache中运行时tomcatv6.0服务器的secong页面即没有加载Welcome.jsp,而第一页即Login.jsp已成功加载。有人可以说编码有什么问题吗? 我在Java 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;
}

}  

的Login.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>

Welcome.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>

3 个答案:

答案 0 :(得分:1)

试试这个。你没有形式,那么你的领域如何发布..

   <%@ 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>

答案 1 :(得分:0)

尝试在IDE中清理项目,然后再次运行服务器。可以工作..

答案 2 :(得分:0)

在Login.jsp。,。

中使用以下代码
<form action="doLogin" method="POST">
<!--
Put all the content of Web Page here.,.
-->
</from>