2

I want to create a simple login form in Struts 2 but I'm having problems at seeing the input fields for some reason and after I submit the name of the user doesn't appear.

Here is the code:

Struts redirects to my struts.xml.

my struts.xml

<!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.enable.DynamicMethodInvocation" value="false"/>
    <!-- devMode equals mode debug information and reload everything for every request -->
    <constant name="struts.devMode" value="true" />
    
    <package name="user" namespace="/User" extends="struts-default">
        <action name="Login">
            <result>Login.jsp</result>
        </action>
        <action name="DashboardAction" class="action.DashboardAction">
            <result name="success">Dashboard.jsp</result>
        </action>
    </package>
    
    
    
    
</struts>

The Bean class

DashboardAction.java:

package action;

public class DashboardAction {
    private String username;
    
    public String execute(){
        return "success";
    }

    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;
}
    
    
    
}

The JSPs

Login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <h1>Struts 2 Login Test</h1>
        
           <form action="DashboardAction" id="form1" method="post" autocomplete="off">
                <div class="input placeholder">
                    <s:textfield name="username" label="Utilizador"/>
                </div>
                <div class="input placeholder">
                    <s:password name="password" label="Password"/>
                </div>
                <div class="submit">
                    <s:submit value="Entrar" method="execute"/>
                </div>
            </form>
   



    </body>
</html>

Dashboard.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome User</title>
    </head>
    <body>
        <h1>Hello
        <s:property value="username"/>
        </h1>
    </body>
</html>

Why doesn't this work after I press submit? It's supposed to go to Dashboard.jsp?

Roman C
  • 49,761
  • 33
  • 66
  • 176
exceltior
  • 103
  • 2
  • 12

3 Answers3

1

Here you go :

 <result name="success">/Dashboard.jsp</result>

You need to specify the full-path to the JSPs in your results. Notice the /. You need to do the same to your other result pages including Login.jsp

coding_idiot
  • 13,526
  • 10
  • 65
  • 116
0

You should map the form to the action that accept data submitted. Use Struts tags

<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="Welcome" ...
  <s:textfield name="username" ...
  <s:password name="unknown" ...

couldn't map the password field because in your action there's not a property. May be if you create it

private String unknown;

public String getUnknown() {
    return unknown;
}

public void setUnknown(String unknown) {
    this.unknown = unknown;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • that didnt help at all... the form is still not appearing or working – exceltior Dec 09 '13 at 19:50
  • This is because your question is incomplete. What url you enter? – Roman C Dec 09 '13 at 19:51
  • its automatically enter by the ide that goes to http://localhost:8084/struts2/ supposedely after i press submit it "post" to Dashboard.jsp the problem is that login form doesnt even appear in the page ... – exceltior Dec 09 '13 at 19:52
  • But you should `localhost:8084/struts2/User/Login` – Roman C Dec 09 '13 at 19:53
  • Thats not even the problem .... it works like this because the page appears ... the login form doesnt ... if i change it to a normal login form without tags it appears and everything works except the name doesnt appear in the Welcome page ... which is normal since i dont use the struts tags and thats why i need to find out why it doesnt appear – exceltior Dec 09 '13 at 19:56
  • `Welcome` is not a page, it's the action that should execute and return a dispatcher result to `Dashboard.jsp`. – Roman C Dec 09 '13 at 19:59
  • thanks i think i understood it now .... any idea why the login form doesnt appear? – exceltior Dec 09 '13 at 20:06
  • You should define struts taglib directive to use Struts tags. – Roman C Dec 09 '13 at 20:09
  • Thank you very much... now the login form appears ... any reason why when i try to submit it appear nullpointerexception? `code java.lang.NullPointerException org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69) com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185) org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)` – exceltior Dec 09 '13 at 20:13
  • you have wrong field names that aren't mapped to the action properties. This stacktrace isn't helpful, rather than I can say that `FilterDispatcher` is deprecated. You could post a full stacktrace from the server logs which include the root cause of the exception. – Roman C Dec 09 '13 at 20:14
  • can you give me an example how to map the field name to a action property? – exceltior Dec 09 '13 at 20:17
  • See in this answer the field named `unknown` is mapped to the action property with the same name. – Roman C Dec 09 '13 at 20:20
  • i mapped like i edited above following your example but still give me nullpointer exception do you know why? – exceltior Dec 09 '13 at 20:26
  • Read my comment above, it could be due project configuration, library set, deployment. Try to clean-rebuild-redeploy if you are in eclipse. – Roman C Dec 09 '13 at 20:29
  • ok thanks for the time you have spent with me ... i will try to fix this ... i tried to clean and everything but still gives me the same error. – exceltior Dec 09 '13 at 20:45
  • Try [this](http://stackoverflow.com/a/18881450/573032) answer or what @coding_idiot is suggested. I can't count errors in your code. – Roman C Dec 09 '13 at 20:53
0

Define property password in your action class like below.

DashboardAction.java

package action;

public class DashboardAction {
    private String username;
    private String password; // you don't have this line in your action class. 
    public String execute(){
        return "success";
    }

    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;
}


}
Prabhakar Manthena
  • 2,223
  • 3
  • 16
  • 30