Hands On Struts2: Ian Roughley, Consultant
Hands On Struts2: Ian Roughley, Consultant
Manually
• Configure web.xml
• Write configuration and code
Using Maven2
• Generate project
• Working baseline
• Integrated servlet engine
Configuration files
• web.xml
• struts.xml
Application elements
• Action class
• JSP template
A way to run the application
<filter>
<filter-name>action2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<action name="index"
class="com.fdar.s2.IndexAction">
<result name="success">/jsp/index.jsp</result>
</action>
</package>
</struts>
<head>
<title>Index</title>
</head>
<body>
<s:form namespace="/" action="index">
<s:textfield label="What is your name?" name="name" />
<s:submit />
</s:form>
</body>
</html>
<struts>
<package name="myPackage" extends="struts-default">
<interceptor-stack name="validationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
<default-interceptor-ref name="defaultStack"/>
</package>
</struts>
<action name="test"
class="com.fdar.actions.MyAction">
<result name="success">/result.jsp</result>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
Built in types
• boolean, Boolean, char, Character, int, Integer, float,
Float, long, Long, double, Double, and Date
<s:textfield label="Name"
name="items[%{#stat.index}].name" />
<s:textfield label="Description"
name="items[%{#stat.index}].description" />
</s:iterator>
Annotations for
• Required Fields, Ranges, Email, Expressions, Regular
Expressions,
URLs, Visitor
Can be configured via XML
validate() method in Validateable
Can create custom validators
@RequiredStringValidator(
message="Please enter a name", trim=true)
public void setName(String name) {
this.name = name;
}
…
}
@Validations( expressions = {
@ExpressionValidator(message="Name cannot be Bob",
expression="name!='bob'" )
})
public String execute() throws Exception {
…
}
}
No changes to JSP
• errorMessage and errorLabel CSS classes
validation and workflow interceptors
Requires a configured INPUT result
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<bean id="userService"
class="com.fdar.s2.services.UserServiceImpl" />
</beans>
Options
• name (default)
• type
• constructor
• auto
<constant value="type"
name="struts.objectFactory.spring.autoWire" />
Configured externally
Decorates HTML via URLs
• Can use META tags to specify template
Configuration options include
• Configuration File
• Agent
• Parameter
• Printable
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<excludes>
<pattern>/styles/*</pattern>
<pattern>/scripts/*</pattern>
<pattern>/api/*</pattern>
<pattern>/*.html</pattern>
</excludes>
</decorators>
<html>
<head>
<title><decorator:title default="Struts Starter"/></title>
<link href="<s:url value='/styles/main.css'/>"
rel="stylesheet" type="text/css" media="all"/>
<decorator:head/>
</head>
<body id="page-home"
onload="<decorator:getProperty property="body.onload"/>">
<decorator:body/>
</body>
</html>
<definition name="indexTile"
template="/tiles/layout.jsp">
<put name="title" value="Tiles Showcase"/>
<put name="header" value="/tiles/header.jsp"/>
<put name="body" value="/tiles/indexBody.jsp"/>
</definition>
</tiles-definitions>
<html>
<head>
<title><tiles:getAsString name="title"/></title>
</head>
<body>
<tiles:insertAttribute name="header"/>
<p id="body">
<tiles:insertAttribute name="body"/>
</p>
</body>
</html>
<div>
<s:form namespace="/search" action="search" method="POST">
<s:textfield label="Search for" name="titlePartial" />
<s:submit label="Go"/>
</s:form>
</div>
<div id="main">
…
</div>
<div>
<s:form namespace="/search" action="search" method="POST">
<s:textfield label="Search for" name="titlePartial" />
<s:submit label="Go" theme="ajax" targets="main" />
</s:form>
</div>
<div id="main">
…
</div>
<action name="index"
class="com.fdar.s2.IndexAction">
<result type="xslt">
<param name="exposedValue">items</param>
</result>
</action>
</package>
</struts>
<action name="index"
class="com.fdar.s2.IndexAction">
<result type="json">
<param name="root">items</param>
</result>
</action>
</package>
</struts>
http://www.apress.com/book/view/978159059903
http://www.infoq.com/minibooks/starting-struts2
Ian Roughley
http://www.fdar.com
[email protected]