Unable to get value from value stack in struts2 using <s:action> - java

What I want: I want to retrieve the value from List collection.
I’m practicing/learning struts 2 framework. But, I am confused about OGNL behavior.
These are my files:
Index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
<hr>
<s:action name="one" />
<s:property value="list_fruits[0]" />
</body>
</html>
MyAction.java
package abc;
import java.util.*;
import org.apache.struts2.dispatcher.RequestMap;
import com.opensymphony.xwork2.*;
public class MyAction extends ActionSupport {
private List list_fruits;
public List getList_fruits() {
return list_fruits;
}
public void setList_fruits(List list_fruits) {
this.list_fruits = list_fruits;
}
public String doOne() {
list_fruits = new ArrayList();
list_fruits.add("banana");
list_fruits.add("apple");
list_fruits.add("mango");
/*RequestMap rm = (RequestMap) ActionContext.getContext().get("request");
rm.put("req_scope", list_fruits);*/
return "sendToOne";
}
}
one.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
<h1>ONE.JSP</h1>
<br>
<s:property value="list_fruits[0]" />
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="devMode" value="true" />
<package name="vns" extends="struts-default">
<action name="one" class="abc.MyAction" method="doOne">
<result name="sendToOne">/one.jsp</result>
</action>
</package>
</struts>
I am experiencing following behaviors:
Case1: When I put this (below) code in index.jsp, I get NO value printed.
<s:action name="one"/>
<s:property value="list_fruits[0]"/>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Case2: When I put this (below) code in index.jsp, the value gets printed from one.jsp because in this scene I included attribute executeResult=”true”
<s:action name="one" executeResult="true"/>
<s:property value="list_fruits[0]"/> <!-- still NOT printed here, but gets printed from one.jsp -->
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Case3: When I put this (below) code in MyAction.java and index.jsp, then the value get printed on screen (index.jsp).
MyAction.java
RequestMap rm=(RequestMap)ActionContext.getContext().get("request");
rm.put("req_scope", list_fruits);
index.jsp
<s:action name="one"/> <!-- removed executeResult="true" -->
<s:property value="#request.req_scope[0]"/>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
in Case2 value gets printed due to one.jsp and not due to index.jsp
I want to know why I am NOT getting any value printed in Case1 whereas in Case2 and Case3, there is no such problem. Why is it so? Can anyone guide me?

AleksandrM already answered you, but since you said
still not clear. :( please explain.
You can use it for :
calling an Action and use executeResult="true" to get the result;
calling an Action and push it on the main page ValueStack with var attribute, then reference it with # :
<s:action name="one" var="instance" />
<s:property value="#instance.list_fruits[0]"/>
calling an Action that sets something in request / session scope, and then retrieve those values by using #attr or #session;
calling an Action that does something (e.g. persists into the database the timestamp of when the page has been opened).
But you can do any of this single things server side, and BETTER.
This is why you shouldn't use the <s:action/> tag: it breaks most of the S2 framework conventions and mechanisms and it is a bad practice (or at least it isn't a good one).

Related

There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts]

Hope someone just could give a hint where to look for the source of the problem.
Class Login Action
enter code here
#Namespace("/")
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
#Override
#Action(value = "/welcome", results = { #Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })
When I exectute my index.jsp to execute welcome.jsp didn't work.
index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome to Struts 2</h3>
<s:form action="home">
<s:textfield name="username" label="User Name"></s:textfield>
<s:textfield name="password" label="Password" type="password"> </s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
welcome.jsp
enter code here
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=IUTF-8">
<title>Welcome To Struts 2</title>
</head>
<body>
<h3>
Welcome
<s:property value="username"></s:property>
!
</h3>
</body>
Result
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
In Struts2 you map a form to the action config using s:form tag's action and namespace attributes.
Normally, the action attribute accepts the action name, but it can contain the URL. The form tag is rendered by the template to HTML form where the action attribute have to be URL.
So Struts is getting the action name and generate URL. You can see the source code in the browser. When request is made Struts2 uses the request URI to find the action mapping as explained here.
Usually the 404 error results in missing action configuration on the server that corresponds to the requested URI. When you submitted a form the request is made and resource is requested but the action config is not found, hence Struts2 returns 404 error.
You don't need to use annotations if you are utilizing a convention or rest plugins but the annotation always allows manually override the defaults.
By default the results path is /WEB-INF/contents/ and the action name is without slashes, so you can use annotation:
#Action(value = "welcome", results = #Result(location = "welcome.jsp") )
You can find the references to online resources from this answer.
The one from tutorials read this tutorial.
The solution.
In my index.jsp I wrote "home" in the tag action, form action="home" The correct are the same name at the Action.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome to Struts 2</h3>
<s:form action="home"> **The correct is "welcome"**
<s:textfield name="username" label="User Name"></s:textfield>
<s:textfield name="password" label="Password" type="password"> </s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
And in my LoginAction I used value ="/welcome".
package web.actions;
#Action(value = "/welcome", results = { #Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })
public String execute() throws Exception {
return SUCCESS;
}
The messagem is very clear.
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
But I just saw the mistake doing a lot of examples.

Page not found error for html file used in jsp project

I am learning Jsp and working on a basic Jsp project which is using a HTML file in it.
When I run the HTML file, it gives page not found error and while running the Jsp, it does not load the form and button in it.
Kindly help me out.
Below are my HTML and Jsp files:
Index.html
<html>
<head><title>New</title></head>
<body>
<form action="myFirstJsp.jsp">
<input type="text" name="uname">
<input type="submit" name="submit">
</form>
</body>
</html>
myFirstJsp.jsp
<%#page import="java.util.Calendar"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>My First JSP</title>
</head>
<body>
<%String name= request.getParameter("uname");
out.println("Welcome " +name);%>
</body>
</html>
First check if your server is up & your app is well deployed on it. Check the request url being fired from the browser.
Also
It's a good practice to use jstl tag to provide urls in your application. It computes the context path averting 404 page not found errors due to incorrect urls.
<form action="<c:url value="/myFirstJsp.jsp">
Add the jstl library in your jsp
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>

How exactly do the jspInit() and other methods work on JSP with Glassfish 4.0?

I am new to JSP. This is my simple JSP file:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.Date"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!public void jspInit() {
System.out.println("Init called!");
}%>
<h3>Hello World!</h3>
<br />
<%
int i;
for (i = 0; i < 100; i++)
out.print(i + " ");
out.println("èéòçàù<br/>");
%>
<b>The time right now is: <%=new Date()%></b>
<%
if (request.getParameter("name") != null) {
session.setAttribute("name", request.getParameter("name"));
application.setAttribute("name", request.getParameter("name"));
}
%>
<br />
<b>The name that was set for request is: <%=request.getParameter("name")%></b>
<br />
<b>The name that was set for session is: <%=session.getAttribute("name")%></b>
<br />
<b>The name that was set for application is: <%=application.getAttribute("name")%></b>
<br />
</body>
</html>
The jspInit() method is called every time my page is opened. Shouldn't it be called only the first time the page is opened? And can somebody please tell me what are the other JSP methods and how do they work? I can't find precise information on this. Thank you!
Use deployment descriptor for setting mapping:
<servlet>
<servlet-name>Index</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Index</servlet-name>
<url-pattern>/Index/*</url-pattern>
</servlet-mapping>
When you opening the page via direct name(index.jsp) the .jsp file isn't working correctly and jspInit() is calling every time when you open the page. But when you open page using url pattern(Index), the page works correctly.

<c:foreach> jstl tag doesn't print anything

The tag doesn't work... Jsp doesn't print anything... Could I debug jsp page in Eclipse?
view
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<ul>
<c:forEach items="${listOfMyFriends}" var="friend">
<c:out value="${friend}"></c:out>
</c:forEach>
</ul>
</body>
</html>
controller are in MyController.java
#Controller
public class MyController {
#RequestMapping(value="/my")
public String getMyHomePage(Model model) {
LinkedList<String> listOfMyFriends = new LinkedList<String>();
listOfMyFriends.add("friend1");
listOfMyFriends.add("friend2");
listOfMyFriends.add("friend3");
listOfMyFriends.add("friend4");
model.addAllAttributes(listOfMyFriends);
return "my";
}
}
Replace the line before the return with
model.addAttribute("listOfMyFriends", listOfMyFriends);
When you do a model.addAllAttributes(listOfMyFriends);
It Copies all attributes in the supplied Collection into this Map, using attribute name generation for each element.
Attribute name generation is done using the method : Conventions.getVariableName()
So, if you are not certain what will be the generated attribute name, use
model.addAttribute("listOfMyFriends", listOfMyFriends);
So that you can access the list of your friends using the key "listOfMyFriends"
Add listOfMyFriends to the Model as follows :
model.addObject("listOfMyFriends ", listOfMyFriends );

(Eclipse) The value for the useBean class attribute is invalid

I have been looking for ages now but none of the solutions google offered me helped for my situation.
I wrote a simple JSP-File and only tried to use the "useBean" statement (that's the line where the error occurs):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="package1.TestBean" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="test" class="package1.TestBean" />
</body>
</html>
My JavaBean is an empty class but has an empty constructor without parameters:
package package1;
public class TestBean {
public TestBean() {
}
}
I created the class by right-clicking the project folder and then using New->Class.
So as I already said, the error occurs in the following line:
<jsp:useBean id="test" class="package1.TestBean" />
And the error message is (as already named in the title) "The value for the useBean class attribute package1.TestBean is invalid."
Please help me! :-(
Edit: A friend of mine tried the same, on his computer it works. Now he sent me his project folder, I imported it and it works, too!?!?!?
Solution found: There was a conflict between two Tomcat installations. See comments on Question for more detailed information.
stop Tomcat,go to Project-->Clean ->select the respective project->click clean. start Tomcat again

Categories