您的位置:首页 > 编程语言 > Java开发

【Struts2】(1)第一个程序

2015-11-26 15:26 411 查看

1. 环境搭建

1. 首先从Apache官网下载Struts的压缩包,将如下几个常用包导入工程:



2. 然后在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>
3. 最后从实例工程中把struts.xml考入src目录下,删去<struts>标签下的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

</struts>

2. 运行第一个程序

1. 首先需要创建一个model类:

package com.thr.struts2.model;

public class MessageStore {

private String message;

public MessageStore() {

setMessage("Hello Struts User");
}

public String getMessage() {

return message;
}

public void setMessage(String message) {

this.message = message;
}

}


这个类需要有一个属性message,还需要有get和set方法。

2. 创建一个Action类继承自ActionSupport:

package com.thr.struts2.action;

import com.opensymphony.xwork2.ActionSupport;
import com.thr.struts2.model.MessageStore;

public class HelloWorldAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private MessageStore messageStore;

public String execute() throws Exception {

messageStore = new MessageStore();
return SUCCESS;
}

public MessageStore getMessageStore() {
return messageStore;
}

public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}

}


这个类有一个MessageStore对象messageStore,并且在执行execute方法后返回一个字符串SUCCESS(实际上就一个常量"success")。

3. 创建用来显示页面的HelloWorld.jsp文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World!</title>
</head>

<body>
<h2>
<s:property value="messageStore.message" />
</h2>
</body>
</html>


这里value中的messageStore.message实际上就是我们上面创建的Action类中的messageStore,然后调用了它的getMessage方法,将结果显示在页面上。

4. 配置我们之前创建好的struts文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">

<action name="index">
<result>/index.jsp</result>
</action>

<action name="hello" class="com.thr.struts2.action.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>


当页面访问action为hello的指令时,会调用HelloWorldAction的execute方法,当返回值为"success"时,页面跳转至/HelloWorld.jsp。这些是在一个action中配置的。

上面action为index的没有配置class和result name,那么它默认会配置class为"ActionSupport",method为"execute",resutl中的name为"success"。

5. 再编辑一下我们的index.jsp文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>


其中<s:url action='hello'>可以生成一个跳转至action指定为hello的路径。

运行程序后,点击Hello World链接,会执行hello的action,由于struts中的配置,它会找到我们创建的Action类,执行execute方法,当成功返回"success"时,页面就跳转到HelloWorld.jsp并将Action中附带的messageStore的message显示在页面上。于是,可以看见Hello
Struts User输出在了页面上。

当struts配置文件中对package进行了namespace的设定时,例如namespace="/a/b",那么在jsp文件中也要指定好namespace="/a/b才可以正常访问。

如果想修改当在浏览器中输入xxx.do或者xxx.其它时,可以在struts中添加配置:

<constant name="struts.action.extension" value="action,,do,webwork"></constant>
关于详细的配置参数都在struts2-core中org.apache.struts2包下的default.properties中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: