Struts 2的 > Hello World应用程序

Hello World应用程序
在本教程中,我们将看到如何创建一个simpe Struts 2的Hello World应用程序。 下面的文件是需要创造一个Hello World应用程序。
web.xml中
struts.xml中
HelloWorld.java的
的index.jsp
success.jsp中
下图显示了Hel​​lo World应用程序的目录结构。

web.xml中

web.xml是用于配置了hello world蒋云良servlet容器的属性。 过滤器和过滤器映射元素用于设置Struts 2的FilterDispatcher。 该过滤器映射到URL模式“/ *”。 这意味着所有的传入请求的目标到Struts 2动作将由FilterDispatcher类处理。
01.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><filter></span> <filter></span>
02.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><filter-name>struts2</filter-name></span> <filter-name> Struts2的</过滤器的名称></span>
03.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class></span> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </过滤器类></span>
04.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></filter></span> </滤镜“></span>
05.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><filter-mapping></span> <filter-mapping></span>
06.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><filter-name>struts2</filter-name></span> <filter-name> Struts2的</过滤器的名称></span>
07.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><url-pattern>/*</url-pattern></span>的<url-pattern> / *“/的url - pattern></span>
08.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></filter-mapping></span> </过滤器映射></span>
09.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><welcome-file-list></span> <welcome-file-list></span>
10.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><welcome-file>index.jsp</welcome-file></span> <welcome-file>的index.jsp </欢迎-档案“></span>
11.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></welcome-file-list></span> </欢迎文件列表></span>
对于我们的Hello World应用网关是index.jsp文件。 index.jsp文件中应当提到如上所示的web.xml。
struts.xml中

该条目指向XML的声明性建筑是struts.xml文件。 在struts.xml文件包含以下动作映射。
1.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><struts></span> <struts></span>
2.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><package name="default" extends="struts-default"></span> <包name="d​​efault" extends="struts-default"></span>
3.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><action name="HelloWorld" class="vaannila.HelloWorld"></span> <action name="HelloWorld" class="vaannila.HelloWorld"></span>
4.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><result name="SUCCESS">/success.jsp</result></span> <result name="SUCCESS"> / success.jsp中</结果></span>
5.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></action></span> </操作“></span>
6.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></package></span> </包“></span>
7.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></struts></span> </ struts的></span>
的index.jsp

Struts 2的UI标签的是简单而有力。 要在JSP页面中使用下面的taglib指令应包括Struts标签。
01.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><%@taglib uri="/struts-tags" prefix="s" %></span> <%@标签库的uri =“/ Struts的标签”前缀=“的”%></span>
02.
 
03.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><html></span> <html></span>
04.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><head></span>的<head></span>
05.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><title>Hello World</title></span>世界的<title>您好</标题></span>
06.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></head></span> </头></span>
07.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><body></span>的<body></span>
08.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><s:form action="HelloWorld" ></span> <s:form action="HelloWorld"></span>
09.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><s:textfield name="userName" label="User Name" /></span> <s:textfield name="userName" label="User Name" /></span>
10.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><s:submit /></span> <s:submit /></span>
11.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></s:form></span> </ s的:表单“></span>
12.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></body></span> </身体></span>
13.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></html></span> </的HTML></span>
HelloWorld.java的

正如你看到的HelloWorld类很简单。 它包含两个属性一个用户名和其他信息显示。
01.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public class HelloWorld {</span>公共类的HelloWorld {</span>
02.
 
03.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private String message;</span>私有String消息;</span>
04.
 
05.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private String userName;</span>私有String用户名;</span>
06.
 
07.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public HelloWorld() {</span>公众的HelloWorld(){</span>
08.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
09.
 
10.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public String execute() {</span>公共字符串的execute(){</span>
11.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">setMessage("Hello " + getUserName());</span> setMessage(“你好”+ getUserName());</span>
12.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return "SUCCESS";</span>返回“成功”;</span>
13.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
14.
 
15.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public String getMessage() {</span>公共字符串GetMessage函数(){</span>
16.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return message;</span>返回的消息;</span>
17.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
18.
 
19.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public void setMessage(String message) {</span>公共无效setMessage(字符串消息){</span>
20.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this.message = message;</span> this.message =信息;</span>
21.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
22.
 
23.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public String getUserName() {</span>公共字符串getUserName(){</span>
24.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return userName;</span>返回用户名;</span>
25.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
26.
 
27.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public void setUserName(String userName) {</span>公共无效setUserName(字串名){</span>
28.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this.userName = userName;</span> this.userName =用户名;</span>
29.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
30.
 
31.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
在execute()的方法,我们的HelloWorld行动撰写邮件显示。 请注意,我们不需要有一个像Struts表单bean 1单独访问表单数据。 我们可以有一个简单的Java类中的行动。 这个动作不需要扩展任何类或实现任何接口。 唯一的责任是,你需要有一个execute()方法返回一个String,并有一个公共的范围。
success.jsp中

我们在成功页面显示“ 您好用户 ”邮件使用属性标记。
01.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><%@taglib uri="/struts-tags" prefix="s" %></span> <%@标签库的uri =“/ Struts的标签”前缀=“的”%></span>
02.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><html></span> <html></span>
03.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><head></span>的<head></span>
04.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><title>Hello World</title></span>世界的<title>您好</标题></span>
05.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></head></span> </头></span>
06.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><body></span>的<body></span>
07.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><h1><s:property value="message" /></h1></span> <h1> <s:property value="message" /> </ h1的></span>
08.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></body></span> </身体></span>
09.
<span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"></html></span> </的HTML></span>
解压到Tomcat的webapps文件夹下载的文件。 启动Tomcat服务器。 键入http://localhost:8080/Example1/index.jsp“下面的URL在浏览器”。 索引页将被显示。

输入用户名并提交表格。 您好用户名信息将被显示。

你可能感兴趣的:(tomcat,jsp,应用服务器,xml,struts)