2

I have learned theory of Struts2 and now practicing. Facing problems while executing project.I searched in Google in many ways but could not find result.Please help me. Below is the code.Please help me friends...

Project structure:

Project structure

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2</display-name>
<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>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

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="resources" value="ApplicationResources" />
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
    <action name="login" class="com.practice.structs.actions.LoginAction"
        method="validateUser">
        <result name="success">pages/homepage.jsp</result>
        <result name="error">pages/login.jsp</result>
    </action>

</package>

LoginAction.java

package com.practice.structs.actions;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String userName;
private String password;
public String validateUser(){
    if(this.userName.equalsIgnoreCase("abc") && this.password.equalsIgnoreCase("abc"))
    {
        return "success";
    }else{
        addActionError(getText("error.login"));
        return "error";
    }
}

/**
 * @return the userName
 */
public String getUserName() {
    return userName;
}
/**
 * @param userName the userName to set
 */
public void setUserName(String userName) {
    this.userName = userName;
}
/**
 * @return the password
 */
public String getPassword() {
    return password;
}
/**
 * @param password the password to set
 */
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" %>
<html>
<head>

<title>Login page</title>
</head>
<body>
<H1><I>Login Page</I></H1>
<s:actionerror />
<s:form action="login.action" method="post">
    <s:textfield name="uname" key="label.username" size="20"/>
    <s:password name="password" key="label.password" size="20"/>
    <s:submit method="execute" key="label.login" align="center"/>
</s:form>
</body>
</html>

homepage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<H2><I>Welcome</I></H2>
</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
user2940073
  • 79
  • 1
  • 1
  • 5

6 Answers6

2

change your code like this

 <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="com.practice.structs.actions.LoginAction"
            method="validateUser">
            <result name="success">pages/homepage.jsp</result>
            <result name="error">pages/![enter image description here][1]login.jsp</result>
        </action>

    </package>


<s:form action="login" method="post">
    <s:textfield name="uname" key="label.username" size="20"/>
    <s:password name="password" key="label.password" size="20"/>
    <s:submit method="execute" key="label.login" align="center"/>
</s:form>

your action name in form is action.login and in struts.xml is login both should be same and also add the namespace

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • But getting same error.[XmlConfigurationProvider] Parsing configuration file [struts-default.xml] [XmlConfigurationProvider] Unable to locate configuration files of the name struts-plugin.xml, skipping [XmlConfigurationProvider] Parsing configuration file [struts-plugin.xml] [XmlConfigurationProvider] Unable to locate configuration files of the name struts.xml, skipping [XmlConfigurationProvider] Parsing configuration file [struts.xml] – user2940073 Dec 17 '13 at 09:05
  • please make sure you have added all the library files,and check the location of struts.xml is it under source package – Lijo Dec 17 '13 at 09:10
  • below are the jars in my application struct2 | |-src |-resources |-struts.xml |-ApplicationResources.properties lib |-commons-fileupload-1.3.jar |-commons-io-2.2.jar |-commons-lang-2.4.jar |-commons-lang3-3.1.jar |-commons-logging-1.1.3.jar |-commons-logging-api-1.1.jar |-freemarker-2.3.19.jar |-javassist-3.11.0.GA.jar |-ognl-3.0.6.jar |-struts2-core-2.3.16.jar |-xwork-core-2.3.16.jar – user2940073 Dec 17 '13 at 09:21
  • please let me know if i need to addd any jars or remove existing jars. – user2940073 Dec 17 '13 at 09:25
  • http://struts.apache.org/release/2.2.x/docs/create-struts-2-web-application-with-artifacts-in-web-inf-lib-and-use-ant-to-build-the-application.html – Lijo Dec 17 '13 at 09:40
  • @404 I have read that link is already outdated, also could you explain what they mean by "artifact"? – Roman C Dec 17 '13 at 09:46
  • In software development life cycle (SDLC), artifact usually refers to "things" that are produced by people involved in the process. Examples would be design documents, data models, workflow diagrams, test matrices and plans, setup scripts, ... like an archaeological site, any thing that is created could be an artifact. – Lijo Dec 17 '13 at 09:56
  • @404 Did they refer to SDLC or what is you are composing songs? – Roman C Dec 17 '13 at 10:14
  • As per Project structure (above image) i have added all jars correctly but still i could not find root cause.Please check above code and provide solution. – user2940073 Dec 18 '13 at 04:29
  • please give the complete error list.hope you already changed the code according to my suggestion – Lijo Dec 18 '13 at 04:43
  • INFO [TomcatDeployer] undeploy, ctxPath=/struts2, warUrl=.../tmp/deploy/tmp3795815829038544849struts2-exp.war/ INFO [TomcatDeployer] deploy, ctxPath=/struts2, warUrl=.../tmp/deploy/tmp8150709624299411222struts2-exp.war/ INFO [XmlConfigurationProvider] Parsing configuration file [struts-default.xml] INFO [XmlConfigurationProvider] Unable to locate configuration files of the name struts-plugin.xml, skipping INFO [XmlConfigurationProvider] Parsing configuration file [struts-plugin.xml] INFO [XmlConfigurationProvider] Unable to locate configuration files of the name struts.xml, skipping – user2940073 Dec 18 '13 at 06:18
  • [XmlConfigurationProvider] Parsing configuration file [struts.xml] [AbstractBeanSelectionProvider] Choosing bean (struts) for (com.opensymphony.xwork2.ObjectFactory) [AbstractBeanSelectionProvider] Choosing bean (struts) for (com.opensymphony.xwork2.factory.ActionFactory) [AbstractBeanSelectionProvider] Choosing bean (struts) for (com.opensymphony.xwork2.factory.ResultFactory) [AbstractBeanSelectionProvider] Choosing bean (struts) for (com.opensymphony.xwork2.factory.ConverterFactory) [AbstractBeanSelectionProvider] Choosing bean (struts)for (com.opensymphony.xwork2.factory.InterceptorFactory) – user2940073 Dec 18 '13 at 06:19
  • i am accessing application through this link : http://localhost/struts2/login.action and getting below error msg. HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login] associated with context path [/struts2]. – user2940073 Dec 18 '13 at 06:23
  • in your struts.xml do you closed the struts tag check it – Lijo Dec 18 '13 at 08:39
2

I know this question is a bit outdated, but I also thought it's worth mentioning, to those who happen to end up stumbling onto this post again and still experiencing the issue; assuming you're 100% sure that your mappings are correct and that your web.xml contains the appropriate filter, try the following:

  1. Stop your Tomcat server
  2. Create a "classes" folder in your "WEB-INF" folder
  3. Move your struts.xml file into the newly created "classes" folder
  4. Right click on your Tomcat Server and select "Clean" - not required, but would recommend doing so.
  5. Start up Tomcat again and hope for the best :-)

As a visual aid, your WEB-INF should end up looking something like this:

enter image description here

If you're still experiencing the issue, double check your struts mappings again, as well as your web.xml

Ryan
  • 31
  • 3
1

I don't have enough points to respond Ryan's comment nor rate him, but what he says is a valid solution in concrete cases, and I am going to tell why.

Sometimes the folders you create in a project are not taken as resources of the application, and you have to configure it.

Let me explain myself with a practical example that may have occurred to some mates who asked for this problem:

When you are developing with Eclipse, maybe you choose another project explorer than the Eclipse's default "Project Explorer", as the "Navigator", for example. Using "Navigator" view, you can create folders, but this folders are not package resources (as they are when you create "package resources" with the default "Project Explorer"), so Struts2 cannot find "struts.xml" file to configure the actions. So, the only folders that your project will process as "package resources" are the ones under WEB-INF as Eclipse do in "Dynamic Web Projects" by default.

So then, be sure of having all the configuration files in a "package resource".

AVI33
  • 11
  • 2
0

In your LoginAction.java properties you took are

private String userName; private String password;

But in you login.jsp you wrote as

<s:textfield name="uname" key="label.username" size="20"/> <s:password name="password" key="label.password" size="20"/>

Change

<s:textfield name="uname" key="label.username" size="20"/> to

<s:textfield name="userName" key="label.username" size="20"/>

I hope this answer solves your problem ...

0

make the changes on struts.xml file,, add namespace="/" attribute in like <package name="default" namespace="/" extends="struts-default">

Karthik Kompelli
  • 2,104
  • 1
  • 19
  • 22
0

To solve this problem I had to create a classes folder in WEB-INF and place the struts.xml file there. Then I placed the .jsp files in the web folder and placed '/' before the .jsp files, for example /x.jsp

J-Alex
  • 6,881
  • 10
  • 46
  • 64
Avant1234
  • 5
  • 3