在这个例子中,我们将学习如何在struts 2中创建一个hello world例子。
使用以下库或工具:
整个工程结构如下图所示:
启动打开 myeclipse,创建一个web工程名称为:struts2-xml-demo,选择 file -> new -> web project ,如下图所示:
在这个项目上添加 struts2 的支持,右键点击 struts2-xml-demo 工程,选择 myeclipse -> add struts capabilities,在弹出的对话框中选择 strut 2.1,如下图所示:
这是一个jsp登录页面,它使用struts2标签来显示用户名,密码输入框和提交按钮。
fie : login.jsp
<%@ page contenttype="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head></head> <body> <h1>struts 2 hello world example</h1> <s:form action="welcome"> <s:textfield name="username" label="username" /> <s:password name="password" label="password" /> <s:submit /> </s:form> </body> </html>
文件: welcome_user.jsp – 一个jsp视图用来页面显示欢迎信息给用户。
<%@ page contenttype="text/html; charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head></head> <body> <h1>struts 2 hello world 示例</h1> <h2> hello <s:property value="username" /> </h2> </body> </html>
对 struts1 和 struts2 有非常相似的ui标签语法,只是在命名html元素,例如,术语有一点不同:
struts 1
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html:form action="welcome"> <html:text property="username"/> </html:form>
struts 2
<%@ taglib prefix="s" uri="/struts-tags" %> <s:form action="welcome"> <s:textfield name="username" label="username"/> </s:form>
一个简单的 struts2 的 action 类,它里面声明的所有业务逻辑。
file : welcomeuseraction.java
package com.h3.user.action; /** * * @author h3.com * */ public class welcomeuseraction { private string username; public string getusername() { return username; } public void setusername(string username) { this.username = username; } // all struts logic here public string execute() { return "success"; } }
在struts2中,action类实现任何接口或扩展任何类不是必需的,但它需要创建一个execute()方法来实现所有的业务逻辑,并返回一个字符串值,告诉用户重定向到哪里。
strut配置文件是用来连接所有的东西在一起。 xml文件名必须是 “struts.xml”。在这个实例中,它位于
file : struts.xml
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="user" namespace="/user" extends="struts-default"> <action name="login"> <result>/login.jsp</result> </action> <action name="welcome" class="com.h3.user.action.welcomeuseraction"> <result name="success">/welcome_user.jsp</result> </action> </package> </struts>
声明包和包含动作类,动作类是不言自明的,但你仍可能会感兴趣下面的新标签:
1. package name=”user”
就在包名,并不真正去关心它。
2. namespace=”/user”
它用于匹配“/user”url模式。
3. extends=”struts-default”
这意味着该包是扩展了struts-default 包组件和拦截器,这是在struts-default.xml中文件中声明的,位于struts2-core.jar 文件的根目录。
配置web应用程序部署描述符(web.xml)文件struts2的集成到web项目。
file web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping></web-app>
在struts2中,可以直接使用.action后缀访问操作类。如下url:
http://localhost:8080/struts2-xml-demo/user/login.action
提交后到 http://localhost:8080/struts2example/user/welcome.action 显示如下: