1>首先在tomcat主目录下的conf文件夹-建立catalina文件夹-建立localhost文件夹-添加一个xml文件,如下图:
内容如下:
<Context docBase="D:/workspace/jiesuanoa/WebRoot" reloadable="false" />这种方式服务器会使用.xml的名字作为path属性的值。
(静态部署指的是我们在服务器启动之前部署我们的程序,只有当服务器启动之后,我们的Web应用程序才能访问。)
注意:我们通过url访问项目时,在端口后加的就不是项目名称了,而是这个xml文件的名称,例:http://localhost:8080/jiesuanoa/login/jiesuan.do
2>导入的jar包。
3>src下的包及类。
4>WebRoot下的内容。
5>类BaseAction里是没有内容的 只是一个格式 为了以后Action中通用的方法都提取到此类中,然后其他Action类都继承与它 。
6>LoginAct类代码。
package com.jiesuanoa.front.web; import com.jiesuanoa.front.BaseAction; public class LoginAct extends BaseAction { public String loginInput() { return "loginInput"; } }
7>本来我们是可以直接在struts.xml中配置action的跳转的,但现在却写了三个文件。
因为package、action、interceptors等配置比较多时,都放到一个struts.xml文件不太容易维护。
因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签引用这些配置文件。
例:struts-login.xml是专门为login这个action配置的,以防项目很庞大时候配置信息都集中到struts.xml中会非常混乱。
注意:用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。
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> <!--基本配置--> <include file="struts-default.xml" /> <!--入口登录相关--> <include file="com/jiesuanoa/front/web/struts-login.xml" /> </struts>
struts-default.xml代码。
有很多时候一个<result>可供很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码见下图,
执行顺序:当一个Action返回的String没有相应的<result>与之对应,Struts2就会查找全局的<result>。
<?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> <!--使struts2能够处理以.do和.action为后缀的url。--> <constant name="struts.action.extension" value="do,action"/> <package name="core-default" extends="struts-default"> <!-- 由于我们访问的是html文件要用到freemark 要进行下面配置 default="true" 就是所有页面都是freemark形式 默认为false是JSP形式--> <result-types> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult" default="true"/> </result-types> <!--全局跳转地址--> <global-results> <result name="loginInput">/WEB-INF/login/login.html</result> </global-results> </package> </struts>
struts-login.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="jiesuan.login" namespace="/login" extends="core-default"> <action name="jiesuan" method="loginInput" class="com.jiesuanoa.front.web.LoginAct"/> </package> </struts>
8>最后贴出web.xml的配置。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <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> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> </web-app>
9>login.html只要注意格式就可以了 内容随便填写 只是为了验证项目的正确与否。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> hello world!! <br> </body> </html>