Why Struts2.
First Example on Struts2:
1. Create one Dynamic Project in Eclipse Say Struts2
2. Put all these below jar file inside WEB-INF\lib folder
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
3. Change your web.xml (WEB-INF\web.xml) file as below:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2 First Example </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>
4. In Struts2/src folder, put these two file:
ClientAction.java
import com.opensymphony.xwork2.ActionSupport;
public class ClientAction extends ActionSupport{
String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String execute() throws Exception{
if(getName().equals("")){
return ERROR;
}
else{
return SUCCESS;
}
}
}
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>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">
<action name="clientAction" class="ClientAction">
<result name="success">/client.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>
</struts>
5. Write some jsp file inside Struts2\WebContent
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="clientAction" method="post">
<s:textfield label="Name" name="name"/>
<s:submit/>
</s:form>
</body>
</html>
client.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
Thank you! <b><s:property value="name"/></b>.
<br><br>
</body>
</html>
error.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Some Error</title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<h1>Error!</h1>
This error page is being shown because any of following reasons:
<ul class="boldred">
<li>Field(s) left blank.</li>
<li>Invalid Data Entered.(For example: String in place of Integer.)</li>
</ul>
</body>
</html>
That's it .........
Add any application Server inside the Eclipse and run .....
If you are getting some error that might be for update code.
So, In Eclipse bottom tab one Server tab is there -> locate your application server -> then right click on Struts1 (Project) -> click on "Clean Module Work Directory".
Now go to internet explorer and type URL:
http://localhost:8080/Struts2
and type your name in text box, you should suppose to get output like this
Thank you! Your Name.
Thanks,
Binod Suman
thx for your sharing, it's very nice
ReplyDelete