small program on struts2 and jsp - java

i have done some learning on struts based on one project that i got.Now i have to build 2 to 3 struts jsp pages.
I have following scenario..
<action name="BackAction" class="ClassnamePath">
<result name="user_validated" type="redirectAction">
<param name="actionName">welcome</param>
</result>
<result name="user_profile_found_in_database">/resources/userprofile.jsp</result>
what does param will tell..what is the significance of param(i do not know about this)
my scenario is like this In the class(ClassnamePath) i have done one java program which bring the data from the database and put the values in the userprofile.java(for example the userprofile has variable members like name,email,phone,pin)
that values are come from database and stored in the object of the class userprofile.
i have a task that whenever the result "user_profile_found_in_database" has been done those values should be presented in the jsp as result of the "user_profile_found_in_database"
Is the result "user_profile_found_in_database" that i mentioned in the action tag correct?
the jsp page should be having userprofile with labels as the fields of the userprofile and values should be in the text boxes.
i do not know anything about jsp pages...even web programming..but i am learning on my own..(i am having one doubt how jsp pages are different from struts jsp pages)
with the above tasks i can learn lot in struts and jsp..
Please give some knowledge on this to build further.

JSP pages are not way different than simple JSP pages and in short Struts2 will provides a set of tags which will help we as an end developer to build application fast as these tags provides a easy to access functionality about various features S2 providing, few of them are
Accessing Value Stack using OGNL
Data conversion from server to client and other way around
Features to access other things like request/response/session in most easy and flexible way.
In end when you browser will render the jsp page it will be simple HTML and S2 tags one using in there application will be converted to the HTML as browser will understand HTML.
regarding second part accessing user profile in your jsp do the following gin your Action class
Create an instance of UserProfile in you action class
Create getter and setter for the UserProfile instance
Fill the value in the user-profile (you will fetch that from your DB call)
when your action will send back the response, S2 will place the user-profile instance on top of Value stack and we can access its properties using S2 tags like
<s:textfield name="user_name" value="%{name}"/>
<s:textfield name="user_age" value="%{age}"/>
here name and age are the properties of your user-profile.param in your redirectAction configuration is being used to provide parameters to the result, for more information about what parameters do please read official document.
redirect-action-result
.

Ya i got your question like this,
what is the exact meaning of param tag?
param tag means:
Struts 2 “param” tag is used to parametrize other tags. However, when you declared the “param” tag, the “parameter value” can be define in two ways :
“value” attribute
Text between the start and end of “param” tag.
For Example:
<param name="fruit">Banana</param>
<param name="fruit" value="Banana"/>
In Your example the 1st case i think
Here see this web site it is present complete structure...
http://www.mkyong.com/struts2/struts-2-param-tag-example/

Related

Showing Database call result in Struts 2

Short Question, Is it possible to make a database call in the first load of home page using Struts 2? I have this old project of mine, just a simple online shop(wasn't able to finish it though) and i recalled that i was having a hard time with my home page outputting products listed in the database since struts needed to trigger an action before doing anything. Can you like preload some actions before proceeding to the home JSP? I'm a beginner in struts and could just do simple redirect and other basic actions.
You have many ways to do it. But most easiest way is mapping a action with the home page URL. Action class should do whatever database loading or other logic and forward to the JSP page.
Use a list method with s:iterator on your home.jsp.
You can use the s:action tag in your home page. There are 2 different ways it could be approached.
the only thing in your home page is the <s:action/> tag and let it render the result of the action: executeResult="true" attribute.
specify the var="myaction" attribute and set executeResult="false". You can then access everything from the action through the var name. for example <s:property value="%{myaction.myproperty}"/>
The docs for the s:action tag can be found here: https://struts.apache.org/docs/action.html

Update the database from submit option

I have a jsp page having a 'submit' option for input.On clicking it i want to update the value of a table called tbIndividual.What can be the best approach to do it?
On jsp page i have somthing like this :
User Name : <%=rs.getString(2)%>
First Name : <%=rs.getString(4)%>
Last Name : <%=rs.getString(5)%>
Email Id : <%=rs.getString(6)%>
Contact : <%=rs.getString(7)%>
<input type="submit" value="ADD"></input>
And now i want to update the value of status of that particular individual from 'NO' to 'WAIT' state.On click of this submit button.
Is making new servlet for this task a good option or doing the code in jsp a better one ?
If i need to make a new servlet then what will be the code for it on jsp page .?Please help.
If you are trying to learn servlet with this project then you should create a separate servlet where you will perform your business logic (e.g addition of element in Db) and jsp should be kept away from business logic because role of jsp is to render the output not to produce the output.
If this is a project for production purposes, then you should ( IMO you must ) opt some web framework. As framework will reduce your effort, headache and increase productivity.
First of all, there are certain things you need to understand while developing web applications in Java. I have a few recommendations and inputs
Please don't use Scriptlets. Scriptlets are deprecated and they make your code clumsy and the maintainance will be hard. Use JSTL
You need to create a form in your html to have a set of variables to push them to the server on clicking submit button. The form will have an action attribute which contains the URL where the request should be sent
Create a Servlet and map with the action URL and write the doPost method which can receive the form parameters and do the business logic whatever changing the status from NO to WAIT or anything else
Please take a note that you need to have Session variables in order to have the store the data in between requests from the same client. eg browser.
Is making new servlet for this task a good option or doing the code in jsp a better one ?
Writing a new servlet is a good option, than doing the business logic in jsp page. jsp is meant for presentation, just view. They shouldn't be used to perform business logic
If i need to make a new servlet then what will be the code for it on jsp page .?
JSP should just have all the necessary html elements and attributes like form, action attribute etc.. to post the parameters in the request to the action URL. Please note the method attribute of form. You should use HTTP POST method for posting form parameters to the server
Note : Finally, Writing Servlets are also NOT recommended. Instead, you should opt for webframeworks like Spring MVC , Struts etc. Please go through the link to understand about web frameworks answered by #BaluC
Hope this clarifies.

jsp pass object to an include

Currently I am having a page with a small content need to be refreshed frequently.
For example:
The jsp content is
Chapter 1 :
section abc:
section def:
section ghi:
Assuming I have a class Chapter and its sections are parameters in it
class Chapter{
Section abc;
Section def;
Section ghi;
}
I am using spring mvc, I am adding the following models
model.addAttribute("chapter", chapter);
model.addAttribute("abc", chapter.getAbc());
model.addAttribute("def", chapter.getDef());
model.addAttribute("ghi", chapter.getGhi());
While calling the main page initially I can load the individual sections by using the jsp:includes , while updating them independently I shall add that particular model to that particular jsp page and update it using ajax.
This works fine.
But
The question is,
How can I change the design so that can I manage the whole with just
model.addAttribute("chapter", chapter);
so that I can use the chapter object to get the abc,def,ghi values instead of explicitly passing them .
The problem is I am not able to pass the individual objects to the included jsps from the chapter object like ..
<jsp:include page="abc.jsp" >
<jsp:param name="abc" value="${ chapter.abc}"/>
</jsp:include>
this is not possible. as I can pass only strings
small problem , long description .. hope I made my point.
You can only pass Strings as request parameters, but you can set any kind of object as request attributes. Request scope (not page scope) beans should also work.

how do i find html of struts?

I am really new and not familiar with this...I am trying to edit this folder with a ton of files in it including .xml....I need to find the html part but it is using "struts" and I don't know much about it other than it being a Java MVC framework...
How can I find the html part? the URL is http://localhost:8080/ContractView.do
I searched inside 1 of the xml file and found this which might be of use:
<action name="DashBoardForm" path="/DashBoard" type="com.manageengine.servicedesk.dashboard.action.DashBoardAction">
<forward name="viewDashBoard" path="/dashboard/Dashboard.jsp"/>
</action>
Any Ideas on how I can get the html?
I want to basically add an extra option to the menu.
I am really new to this and just trying to add an extra menu to the navigation bar so that I can embed php files in it that it will link to..
it is a software, can be downloaded at
http://www.manageengine.com/products/service-desk/download.html
/dashboard/Dashboard.jsp should be a view page containing html. In Struts the struts-config.xml file dictates which request gets served by which servlet. In your case, ContactView.do should have a mapping to an action class. Look for the xml config line in that file that contains path="/ViewContact".
In the actual jsp page you may see a mix of struts constructs - (ie. s:<some_tag>), JSTL (ie. c:<some_tag>, and pure html). However, they all obviously get translated to HTML when you are viewing it in a client (ie. browsers).
Another thing you may want to check out is whether it is using tiles. A lot of times the view page may be defined in the tiles-defition.xml file. So that may be another place for you to look where ContactView jsp may be defined at. If it is using tiles then you will find the name of tile definition in the struts-config.xml and then find the actual JSP path in the tiles-definition.xml.
UPDATE
After you added the link to struts-config.xml, I can tell you exactly which JSP your request goes to which is originally what you asked in your post.
Take a look at the following snippet:-
<action name="ContractViewForm" path="/ContractView" scope="request" type="com.adventnet.servicedesk.contract.action.ContractViewAction" input="ContractListView.jsp">
<forward name="contractListView" path="/contract/ContractListView.jsp?task=ContractListView"/>
<forward name="newContract" path="/contract/ContractDef.jsp?task=ContractDef"/>
</action>
<form-bean name="ContractViewForm" type="com.adventnet.servicedesk.contract.form.ContractViewForm"/>
From this snippet as you can see depending on how the page was forwarded from the previous action (ie. form submit) it will either go to /contract/ContractListView.jsp or /contract/ContractDef.jsp . The backing action class is com.adventnet.servicedesk.contract.action.ContractViewAction which is the class where all the business logic processing starts. The form is com.adventnet.servicedesk.contract.form.ContractViewForm where all the form fields are stored and it is at requqest scope.
Hope this helps!

How to JSP display multiple page with one layout (Example index.jsp?page=about display about.jsp)

I new in JSP, i have a problem with JSP
in php i use
$page=$_GET["page"]
for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about
in PHP when i declare $page above and next step i do
Switch($page){
case 1:about
include 'aboutus.php'
case 2:news
include 'news.php'
}
How can i do it ?
How jsp can do the same way php to display multiple page in 1 layout
Use jsp:include.
<jsp:include page="/WEB-INF/${param.page}.jsp" />
And pass ?page=news or ?page=about, etc as parameter. The ${param.page} prints the outcome of request.getParameter("page"). You can prevent direct access to JSP files (by entering URL in browser address bar) by placing JSP files in /WEB-INF folder.
See also:
Basic JSP/Servlet tutorials
Hidden features of JSP/Servlet
How to avoid Java code in JSP
nowadays you use "templates" of Java Server Faces (JSF) for this approach. When you use JSP, you actually don't use the same concept as in PHP. You'd better use the MVC concept. But to answer your question, you could probably achieve this with the include tag http://java.sun.com/products/jsp/tags/11/syntaxref1112.html and control it with JSTL:
http://www.java2s.com/Code/Java/JSTL/JSTLiftag.htm

Categories

Resources