Struts 2 Tutorial cont. w/ Accessing Input Parameters

Data transfer between input URL request and Action class------>http get request

It could be available by mapping a private field in the action class to the execute() method body through the help of Struts interceptor. The URL input parameters will be stored to the related action class private field varible by the public setter function(used to do the value assignment from the URL parameters to Action class private field variables) of the action class . So basiclly the value stack helps the data transfer from bom both input from URLs and output to JSPs

Post requests to actions------>http post request

same handling way as the http get request.

The struts form using struts tags and the http default form.

 firstly to add struts taglib; <%@ taglib prefix="c" uri="/struts-tags" />

 Then using Struts form as below:

<s:form action="no .action needed">

<s:testfield (lable="enter the language your want") key="(the field variables to be populated)" /> 

<s:submit />

</s:form>

before i wind up, the <.action> extension does not need to be in place

 

Login Action and Best Practices

Login JSP page is created

<s:password label="password" key="password">

Loginaction class is created with getter and setter methods in place

New package with new namespace name<namespace="/"> and package name <package name= "login"> should be added to the structs xml file.

Not hard-code the return value instead of using the <private static String SUCCESS="success">

The Struts provides the both class to extend and the interface(Action which contains a  lot of static common Strings) to implements for the action class in order to decrease the redundent code reusage.

It is allowed to have multi xml files which could be included in the struts.xml file rather than the only main structs.xml to make it more organized;

So create a new xml file named login_struts.xml which only include the login package as a particular unit or business group;

In the main struts.xml file, it could add such line like, <include file="login.xml">

Use alias and dummy action class  --->this needs  to be double checked.......

<package name = "search" namespace="/" exttends="">

<action name="searchForm">

<result>/searchForm.jsp</resuot>

</action>

</package>

Action Wildcards

 

Single action name is mapped to multi actions;

STRUTS checks based a top-down order from within the strusts.xml when doing wildcards mapping

----For result wildcard match

<action name="search*">

<result>/search{1}.jsp</result>

</action>

----For class wildcard match

<action name="get*" class="com.infor.{1}Action">

<result>/search{1}.jsp</result>

</action>

 

The ActionSupport Class

Writing strust 2 action and attch the resource to the eclipse so you could see the source code:

POJO ;

Action interface ;

ActionSupport class ;

<result name="input">/login.jsp</result>

Public class LoginAction extend ActionSupport

{

 

}

What should be paid attention to is the <public validate()> method in the actionsupport class which will be executed before the running of execute() method;

public void validate()

{

if stringUtil.isEmpty(userId)

{

addFieldError("userid, ""User ID connot be blank")

}

if stringUtil.isEmpty(password)

{

addFieldError("userid", "password"connot be blank")

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值