404 page when running project through eclipse on Tomcat - java

Using Struts2 , I have set up a very simplistic webapp but when i run it in eclipese i got 404 error.. here is my all codes and jarfiles
URL : localhost:8985/firststruts/index.jsp
Jar files
commons-fileupload-1.3.1
commons-io-2.2
commons-lang-2.4
commons-logging-1.1.3
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.6
struts2-core-2.3.16.3
xwork-core-2.3.16.3
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="Product" class="com.javatpoint.Product">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>firstjsp</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>
</web-app>
product.java
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String execute() {
return "success";
}
}
welcome.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/><br/>
index.java
<%# taglib uri="/struts-tags" prefix="s" %>
<s:form action="product">
<s:textfield name="id" label="Product Id"></s:textfield>
<s:textfield name="name" label="Product Name"></s:textfield>
<s:textfield name="price" label="Product Price"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
im new to struts2 so please help me to solve this.

Related

Execution of Struts basic application getting java.lang.NoSuchMethodException

I'm new to struts framework. So seeking some online tutorials and tried to develop a very basic application. But I'm unable to run it properly
after clicking button action method "execute()" was unable to locate
by container.
I am not getting what I'm missing.
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SampleApplication</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>sample</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>sample</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
index.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>Sample Struts Application</title>
</head>
<body>
<s:form action="invoke">
<s:textfield name="name" label="UserName"></s:textfield><br>
<s:password name="password" label="Password"></s:password><br>
<s:submit value="GO"></s:submit>
</s:form>
</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>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default"><action name="invoke" class="com.techm.SampleStruts" method="execute">
<result name="success">/Success.jsp</result>
</action>
</package></struts>
SampleStruts.java
import com.opensymphony.xwork2.ActionSupport;
public class SampleStruts extends ActionSupport{
private static final long serialVersionUID = 1L;
String name, password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()
{
return "success";
}
}
Jars used are :
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
servlet-api-2.5.jar
struts2-core-2.0.11.jar
StrutsInterceptors.rar
xwork-2.0.4.jar
Stack trace :
java.lang.NoSuchMethodException: com.techm.SampleStruts.execute()
at java.lang.Class.getMethod(Unknown Source)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.java:55)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:41)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
Finally i found the solution is that , after adding .action to the action value.
it is working fine.
<s:form action="invoke.action">
<s:textfield name="name" label="UserName"></s:textfield><br>
<s:password name="password" label="Password"></s:password><br>
<s:submit value="GO"></s:submit>

Struts2 with maven no Action mapped for namespace and action name associated with context path

Although it is a common problem but I cannot figure it out.
I am running Strut2 with maven project. My project structure is given bellow
is
Product.java
package beans;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
ProductAction.java
package actions;
import beans.Product;
public class ProductAction{
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
private Product product;
public String execute(){
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="product" class="actions.ProductAction" method="execute">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
<div>
<%# taglib uri="/struts-tags" prefix="s" %>
<s:form action="product">
<s:textfield name="product.id" label="Product Id"></s:textfield>
<s:textfield name="product.name" label="Product Name"></s:textfield>
<s:textfield name="product.price" label="Product Price"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
</div>
</body>
</html>
and welcome.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%# taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/><br/>
</body>
</html>
I am using Glassfish server. It runs index.jsp well. But when I fill the form and submit it shows
HTTP Status 404 - There is no Action mapped for namespace [/] and
action name [product] associated with context path [/deploytest].
type Status report
messageThere is no Action mapped for namespace [/] and action name
[product] associated with context path [/deploytest].
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 4.1.1
My pom.xml has dependency
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.28</version>
</dependency>

Struts2 action always returning success

I'm a beginner in STRUTS2 and I want to test two Actions.
The first Action Product work correctly. When I enter "hello" return success otherwise error.
But the second Action CatererTyp return always success.
Can somebody please explain me why?
Here is Product.java
package com.javatpoint;
import com.opensymphony.xwork2.ActionSupport;
public class Product extends ActionSupport {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String execute() {
if(name.equals("hallo")){
return SUCCESS;
}
else{
return ERROR;
}
}
}
CatererType.java
package com.javatpoint;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.opensymphony.xwork2.ActionSupport;
public class CatererType extends ActionSupport {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String excute() {
if (description.equals("hello")) {
return ERROR;
} else {
return SUCCESS;
}
}
}
accessdiened.jsp
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
You are not authorized to view this page.
</body>
</html>
welcome.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
The Caterer Type: <s:property value="description"/> was inserted <br/>
adminpage.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
<html>
<center>
<s:form action="caterertype">
<s:textfield name="description" label="Caterer Type"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
</center>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default">
<action name="product" class="com.javatpoint.Product" method="execute">
<result name="success">welcome.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
<action name="caterertype" class="com.javatpoint.CatererType" method="execute">
<result name="success">welcome.jsp</result>
<result name="error">AccessDenied.jsp</result>
</action>
</package>
</struts>

Liferay6.2 : DisplayTag in Struts2 - "Nothing to display"

Here is my problem :
I'm using a Liferay 6.2, with Struts2
I want to use DisplayTag for pagination, but the table doesn't appears, and Liferay shows me this message "Nothing found to display."
I need to show 3 students in a HTML Table :
I put all of the code (bellow) in a simple and classic "Dynamic Web Project" with a Tomcat7, and I can see my 3 students.
But here, with Liferay, I've got the message "Nothing to display."
Web.xml :
<web-app [...]>
<display-name>HelloStruts</display-name>
<display-name>Struts 2 Web Application</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>
</web-app>
Struts.xml :
<struts>
<constant name="struts.devMode" value="true" />
<package namespace="/view" extends="struts-portlet-default" name="view">
<action name="viewDisplayTags" class="com.displaytags.StudentAction" method="fetchStudentList">
<result name="success">/html/displaytags/viewDisplayTags.jsp</result>
</action>
</package>
</struts>
StudentAction.java :
public class StudentAction extends ActionSupport {
private List<StudentBean> students = new ArrayList<StudentBean>();
public String fetchStudentList() {
students.add(new StudentBean("o7bb002", "Ajay"));
students.add(new StudentBean("o7bc055", "Mohiadeen"));
students.add(new StudentBean("o7bc074", "Sriram Ganesh"));
return SUCCESS;
}
public List<StudentBean> getStudents() {
return students;
}
public void setStudents(List<StudentBean> students) {
this.students = students;
}
}
StudentBean.java
public class StudentBean {
private String rollNo;
private String studentName;
public StudentBean(String rollNo, String studentName) {
this.rollNo = rollNo;
this.studentName = studentName;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}
viewDisplayTags.jsp
<%#taglib prefix="s" uri="/struts-tags" %>
<%# include file="/html/init.jsp" %>
<%# include file="headerDisplayTags.jspf" %>
<%# taglib prefix="display" uri="http://displaytag.sf.net"%>
<s:hidden name="studentsHiddenList" value="%{students}" />
<display:table name="students">
<display:column property="rollNo" title="Roll No" sortable="true"></display:column>
<display:column property="studentName" title="Student Name" sortable="true" ></display:column>
</display:table>
That's strange, because with the content of the "hidden" (in "viewDisplayTags.jsp") I can see my 3 students ... The Students List is transmitted from the Java Class to the JSP
Thank you !
I've finally found the answer :
The List "Students" wasn't in the "request" and the attribute class and requestURI in "" wasn't set correctly
StudentAction.java :
public class StudentAction extends ActionSupport {
private List<StudentBean> students = new ArrayList<StudentBean>();
public String fetchStudentList() {
students.add(new StudentBean("o7bb002", "Ajay"));
students.add(new StudentBean("o7bc055", "Mohiadeen"));
students.add(new StudentBean("o7bc074", "Sriram Ganesh"));
Map request = (Map) ActionContext.getContext().get("request");
request.put("students",students);
return SUCCESS;
}
public List<StudentBean> getStudents() {
return students;
}
public void setStudents(List<StudentBean> students) {
this.students = students;
}
}
viewDisplayTags.jsp
<%#taglib prefix="s" uri="/struts-tags" %>
<%# include file="/html/init.jsp" %>
<%# include file="headerDisplayTags.jspf" %>
<%# taglib prefix="display" uri="http://displaytag.sf.net"%>
<s:hidden name="studentsHiddenList" value="%{students}" />
<display:table name="students" class="StudentAction" requestURI="/viewDisplayTags.jsp">
<display:column property="rollNo" title="Roll No" sortable="true"></display:column>
<display:column property="studentName" title="Student Name" sortable="true" ></display:column>
</display:table>

How to forward result to another action in Struts2?

I have this action:
package com.test;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action{
private String simpleParam;
public String getSimpleParam() {
return simpleParam;
}
public void setSimpleParam(String simpleParam) {
this.simpleParam = simpleParam;
}
#Override
public String execute() throws Exception {
return SUCCESS;
}
}
When it's executed I want to invoke another action inside struts(e.g. not redirect) and pass to it simpleParam. SecondAction is:
package com.test;
import com.opensymphony.xwork2.Action;
public class HelloAction implements Action {
private String id;
private String result;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getResult() {
return result;
}
#Override
public String execute() throws Exception {
result = "result" + getId();
return SUCCESS;
}
}
I saw working example when in struts.xml in result simply typed another action name and params and it worked. So I'm trying to do this:
<struts>
<package name="main" extends="struts-default">
<action name="test" class="com.test.TestAction">
<result name="success">hello.action?id=${simpleParam}</result>
</action>
<action name="hello" class="com.test.HelloAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>
Idea totally sees this action but in browser I get 404 status. When I simply invoke hello.action from browser it works. Redirect also works. I also tried chain, but my param wasn't passed and it's not very convinient.
Am I doing it right? And if so what could be the cause of 404 status?
this could be helpful: http://struts.apache.org/docs/action-chaining.html
<struts>
<package name="main" extends="struts-default">
<action name="test" class="com.test.TestAction">
<result name="success" type="chain">hello</result>
</action>
<action name="hello" class="com.test.HelloAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>
to pass a parameter from one action to another you could use the HttpServletRequest
(http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2):
TestAction.java:
package com.test;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action{
private String simpleParam;
public String getSimpleParam() {
return simpleParam;
}
public void setSimpleParam(String simpleParam) {
this.simpleParam = simpleParam;
}
#Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("id", simpleParam);
return SUCCESS;
}
}
HelloAction.java:
package com.test;
import com.opensymphony.xwork2.Action;
public class HelloAction implements Action {
private String id;
private String result;
public String getId() {
return id;
}
public String getResult() {
return result;
}
#Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
id = (String)request.getAttribute("id");
result = "result" + getId();
return SUCCESS;
}
}
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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%# 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>Insert title here</title>
</head>
<body>
View Records all
</body>
</html>
edit.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
<b>id :</b> <s:property value="id"/> <br>
<b>name :</b> <s:property value="name"/> <br>
<b>salary :</b> <s:property value="salary"/> <br>
<s:textfield name="userid" label="phoneno" value="id"/>
UserAction.java
package com.action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.model.User;
import com.userservice.UserService;
public class UserAction {
private int id;
private String name;
private int salary;
private int phoneno;
ArrayList<User> list=new ArrayList<User>();
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public String execute(){
UserService service= new UserService();
list=service.getdetails();
return "success";
}
public String delete() {
System.out.println("----------");
return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getPhoneno() {
return phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
}
UserService.java
package com.userservice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.model.User;
public class UserService {
ArrayList<User> list=new ArrayList<User>();
public ArrayList<User> getdetails(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe","oracletest","oracle");
PreparedStatement ps=con.prepareStatement("select * from employee");
ResultSet rs=ps.executeQuery();
while(rs.next()){
User user=new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setSalary(rs.getInt(3));
user.setPhoneno(rs.getInt(4));
list.add(user);
System.out.println(rs.getString(2));
}
con.close();
}catch(Exception e){e.printStackTrace();}
return list;
}
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
}
User.java
package com.model;
public class User {
private int id;
private String name;
private int salary;
private int phoneno;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getPhoneno() {
return phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
}
welcome.jsp
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts2 Example</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>salary</th>
<th>phoneno</th>
</tr>
<s:iterator value="list" var="user">
<tr>
<td><s:property value="id"/> </td>
<td><s:property value="name"/></td>
<td><s:property value="salary"/></td>
<td><s:property value="phoneno"/></td>
<td>edit</td>
<td>edit1</td>
</tr>
</s:iterator>
</table>
</body>
</html>
</html>

Categories