How to use struts2 for program development?

1. Load the class library of struts2.

the class library contain following jar package:
(1) struct2-core-xxx.jar     -- Core class library
(2) xwork-core-xxx.jar       -- xWork class library, basic of building struts2
(3) ognl-xxx.jar             -- Language expression library
(4) freemarker-xxx.jar       -- Class library for label template
(5) javassist-xxx.GA.jar     -- Class library for dealing bytecode
(6) commons-fileupload-xxx.jar -- Class library for uploading files
(7) commons-io-xxx.jar       -- Expansion library for Java IO
(8) commons-lang-xxx.jar     -- Class library containing utility class with Data Type

2. Configure the file named "web.xml"

<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>

Reason: Position all requests to the specified filters


3. Develop pages in view layer

<s:property value="message"/>
<form action="helloWorld.action" method="post">
	<input name="name" type="text"/>
	<input type="submit" value="submit"/>
</form>

4. Develop Action in controller layer

public class HelloWorkAction implements Action {
	private String name ="";         // Get value from <input name="name" type="text"/>
	private String message = "";     // Populate the content to <s:property value="message"/>
	@override
	public String execute() {
		this.setMessage("Hello" + this.getName() + "!");  // set the content and got by <s:property value="message"/>
		return "success";            // server-side turn page
	}


	... // omit setter and getter methods
}


5. Configure the file named "struts.xml" 

<struts>
	<package name="default" namespace="/" extends="struts-default">
			<!-- contact form and class, form name is helloWorld.action -->
			<action name="hellowWorld" class="cn.jbit.strutsdemo.HelloWorldAction">
				<!-- return to "success" is the same as request.getRequestDispatcher("helloWorld.jsp").forward(request, response) -->
				<result name="success">helloWorld.jsp</result>
			</action>
		</action>
	</package>
</struts>


6. Deploy and run the program

Run with tomcat

------------------------Questions-------------------------------

1. What's the usage of package label in the file named "struts.xml"?

package actions and inherit or be inherited by  other package 

2. Where to place the file named "struts.xml"?

1. default path: 

src/

2. configuring path in web.xml:
when configure struts filter, add following context in filter label.
<filter>
...
<init-param>
<param-name>config</param-name>
<param-value>../conf/struts.xml</param-value>
</init-param>
</filter>

------------------------Conclusion-------------------------------

First of all, I import struts2 related jar package as others' Jave Frameworck do. secondly, I configure the existing file named "web.xml" for position all requests to the specified filters. thirdly, I configure a new file named "struts.xml", in which I contact view page "helloWorld.jsp" and controller action class"HelloWordAction". fourthly, I edit the content of helloWorld.jsp, I add a property label <s:property value="message"> to get the content of HelloWordAction::message. I also add a form label to submit user name inputed by user himself to HelloWordAction::name. I set message in HelloWordAction class and return "success", "success" has reflected to helloWorld.jsp in the file name "strut2.xml". So after I excute the "excute" method in HelloWordAction class, there will be turning to helloWorld.jsp.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值