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

Struts2(三):新建Struts2工程

2016-06-07 00:04 627 查看
下载的struts2xx-all.zip包对搭建项目的作用

一般情况下,我们下载一个Struts2的full包时,并不知道新建过程中需要哪些包,那么这时我们可以从下载的包中解压出的目录\apps\struts2-blank.war文件找解决方案。

我从http://struts.apache.org/下载的是struts-2.3.28-all.zip包,解压到本地后目录结构如下:



apps目录下包含文件及作用:



struts2-blank.war 它可以告诉你如何搭建一个最最简单的Struts2的项目;它还会告诉你,Struts2至少需要依赖哪些jar包(请以后不要再为jar包错误而苦恼);同时,也给你做出了一些范例,web.xml怎么写,struts.xml又怎么写。

struts2-mailreader.war 给出了注册流程、以及发邮件功能

struts2-portlet.war 则给出了在Portal环境下的Struts2的应用。

struts2-rest-showcase.war 讲述了Restful的用法示例。

struts2-showcase.war 这个项目,你则可以看到Struts2的特性的大杂烩,这对于你看reference是相当有帮助的。比如说,你在看文档时看到了"文件上传"的章节,那么你就可以参考项目中的upload子目录。

怎么使用:

1.可以把这些文件拷贝到tomcat的webapps下,之后访问:http://localhost:8080/struts2-blank,之后去webapps下可以查看具体的发布文件夹;

2.也可以当做压缩包来解压出具体的发布文件夹。

以下将会介绍怎么新建一个struts2的工程:

1、使用eclipse创建一个Dynamic web project.



2、添加struts2需要的jar包。

将struts2-blank.war解压后\struts2-blank\WEB-INF\lib下jar包,拷贝到工程\WebContent\WEB-INF\lib文件夹下。



3、修改web.xml

将struts2-blank.war解压后\WEB-INF\web.xml拷贝到新建工程MyStruts001的\WebContent\WEB-INF\web.xml中,并修改为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>Struts Blank</display-name>

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

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<!-- Restricts access to pure JSP files - access available only via Struts action -->
<!--<security-constraint>
<display-name>No direct JSP access</display-name>
<web-resource-collection>
<web-resource-name>No-JSP</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>no-users</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<description>Don't assign users to this role</description>
<role-name>no-users</role-name>
</security-role>
-->
</web-app>


3、新建struts.xml相关文件到Src下。

把struts-blank\WEB-INF\src\java\下的struts.xml,log4j2.xml,velocity.properties拷贝到MyStruts001的\src下。



4、新建一个struts2的Action类。



5、修改struts.xml文件如下:

<?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.action.extension" value="action" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="default"  namespace="/MyStruts001" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/pages/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="project-info">
<result>/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="com.dx.struts001.actions.ProjectAction">
<result name="detail">/WEB-INF/pages/result.jsp</result>
</action>
</package>
</struts>


6、新建相关view页面。

default.html

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="project-info.action"> hellword.action </a>
</body>
</html>


WEB-INF/pages/input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="POST">
<label>name:</label>
<input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>


WEB-INF/pages/result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>


7、运行项目,之后访问地址:http://localhost:8080/MyStruts001/

界面为:



点击"helloword.action",界面为:



输入信息,点击“提交”按钮.



到此结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: