Struts2 First Example.
1. Create one Dynamic Project in Eclipse Say StrutsAnno
2. Put all these below jar file inside WEB-INF\lib folder
struts2-core-2.1.6.jar
xwork-2.1.2.jar
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-convention-plugin-2.1.6.jar
3. Change your web.xml (WEB-INF\web.xml) file as below:
<?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>
There is no use of struts.xml if you use Annotation. :)
4. Create one package inside src folder say binod.suman
UserAction.java (StrutsAnno\src\binod\suman\UserAction.java)
package binod.suman;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
public class WelcomeUserAction {
private String userName;
private String message;
@Action(value="/welcome",results={@Result(name="success",location="/successPage.jsp"),@Result(name="error",location="/error.jsp")})
public String execute() {
message = "Welcome " + userName + " !";
System.out.println("Message : "+message);
if(userName.equals("Binod")){
return "success";
}else{return "error";}
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName() {
return userName;
}
public String getMessage() {
return message;
}
}
5. These below JSP page inside the WebContent
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">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:form action="welcome" >
<s:textfield name="userName" label="User Name" />
<s:submit />
</s:form>
</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>
<h1> You have entered <font color="red"> <b><s:property value="userName"/></b> </font> but you should suppose to enter <b>Binod</b> </h1>
</body>
</html>
successPage.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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome User</title>
</head>
<body>
<b><s:property value="message"/></b>.
</body>
</html>
Only one web.xml, one java class and some jsp pages.
http://localhost:8080/StrutsAnno/
Put UserName Binod
Output:
Welcome Binod !.
That's it ............... :)
Thanks,
Binod Suman
ITS NOT WORKING
ReplyDeleteI think in annotation example Java file name should be WelcomeUserAction.java.This has waisted my two hours.
ReplyDeletePlease send me the complete code folder with jar files.My email id is gvsbaluja@yahoo.com.I will be thankful to you.