第一个Struts2 ,特此记录下配置环境

来源:互联网 发布:上海博科软件 编辑:程序博客网 时间:2024/06/05 11:26

几经周折,总算把struts2给配置好了,并有了一个简单的例子可以运行在浏览器中了,由于java web纯粹个人喜欢,所以大部分东西都是参照网上的资料进行亲身实践,PS:一个自学java希望有朝一日称为java大神的Delphi开发者


不多说了,既然是白,就从最开始说起,先实现一个简单例子,跑在tomcat服务器上:

(1)官网下载tomcat

(2)将下载好的apache-tomcat-8.5.16-windows-x64.zip解压,然后移动解压后的文件夹

(3)eclipse中配置tomcat,选择菜单栏Window-->Preferences,找到server选项卡,然后按图进行服务器运行时环境的的添加

(4)我的eclipse支持T8.5,而我下载的T包也是8.5,所以直接Next,进入下面的配置

(5)ok,上面基本上就是最简单的Tomcat配置了,复杂的这里就不讨论了,点击“Finish“完成后,控制台就会出现一个server标签

此时说明,tomcat已经可以工作了,只差一步举例说明了,接着,我们新建一个web项目MyWebTest,选择Dynamic web project


继续next..

点击完成后,看下项目树:


(6)我们看下web.xml都写了什么


再添加首页面之前,我们先启动tomcat服务,在浏览器中输入localhost:8080进行测试,看看服务是不是有效?

A:启动tomcat服务



如果服务成功启动,console控制台会显示成功的启动日志:

七月 28, 2017 3:07:45 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Struts2Test' did not find a matching property.
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/8.5.16
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Jun 21 2017 17:01:09 UTC
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         8.5.16.0
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 7
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            6.1
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             D:\jdk1.8.0_66\jre
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           1.8.0_66-b17
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         D:\tomcat
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         D:\tomcat
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54805
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\tomcat
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\tomcat
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\tomcat\wtpwebapps
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=D:\tomcat\endorsed
七月 28, 2017 3:07:45 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=GBK
七月 28, 2017 3:07:45 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\jdk1.8.0_66\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/jdk1.8.0_66/jre/bin/server;D:/jdk1.8.0_66/jre/bin;D:/jdk1.8.0_66/jre/lib/amd64;D:\jdk1.8.0_66\jre\bin;D:\jdk1.8.0_66\bin;D:\app\lenovo\product\11.2.0\dbhome_1\bin;d:\Program Files (x86)\Embarcadero\Studio\17.0\bin;C:\Users\Public\Documents\Embarcadero\Studio\17.0\Bpl;d:\Program Files (x86)\Embarcadero\Studio\17.0\bin64;C:\Users\Public\Documents\Embarcadero\Studio\17.0\Bpl\Win64;C:\Inprise\vbroker\bin;C:\Program Files (x86)\Borland\Delphi7\Bin;C:\Program Files (x86)\Borland\Delphi7\Projects\Bpl\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft\Web Platform Installer\;D:\DevExpress\Bin\;D:\app\lenovo\product\instantclient_11_2;D:\ant-1.6.5\bin;D:\JacORB_1_4_1\bin;C:\Program Files (x86)\IBM\WebSphere MQ\bin64;C:\Program Files (x86)\IBM\WebSphere MQ\bin;C:\Program Files (x86)\IBM\WebSphere MQ\tools\c\samples\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Python36-32;E:\Apache24\bin;;E:\QQPCmgr\Desktop;;.]
七月 28, 2017 3:07:45 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
七月 28, 2017 3:07:45 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
七月 28, 2017 3:07:45 下午 org.apache.coyote.AbstractProtocol init
................................................

...............................................

...............................................

然后,浏览器查看效果:

在项目中添加一个index.jsp页面:

然后选择,控制台中的Tomcat server,右键选择"Add and Remove...":

点击完成后,再次将Tomcat server 启动起来,然后通过浏览器,输入http://localhost:8080/MyWebTest/  查看效果,注意之前提到的,首页index.jsp不需要输出:

看到这个,关于Tomcat的环境配置就告于段落了,下面着重讲struts2的环境搭建:

(一)下载struts  官网点击打开链接



,下载后,存放到任意盘,然后解压:


将复制的jar包拷贝到项目中:


配置好后,启动服务的时候,tomcat会自动将lib重的包加载到项目中去的,所以不用将这些jar包在 add build to path 了,这些包的引入,就已经宣告了struts2已经加进当前的web项目中了,但是具体用,还需要三步走:

A:web.xml配置 struts过滤器,struts2核心技术就是过滤器,我简单的理解一下:比如A和B通话,不是直接通话的那种,需要用到媒介,假设是手机吧,再假设手机中对于通话有个监视器,监视器的作用就是监听通话的内容,如果监听器认为A传达的语言很黄很暴力的话,就会pass掉说话的内容,此时另一端的B就不可能接收到A刚刚说的话,反之则可以。 当然这也只能简单说明了struts2过滤器的作用,实际上它的强大之处简单举个例子是说明不了(虽然目前我还不知道它有多强,哈哈):


配置好web.xml后,接下里我们要

B:为过滤器添加行为,也就是我们要干什么(自己理解)

(1)首先我们需要一个登陆界面Login.jsp,登陆界面有用户名和密码输入,然后提交,我们可以在提交之后,对数据做个验证,如果用户名等于"appleyk",密码等于"521",我们就让他跳转到欢迎界面Welcome.jsp,如果用户名和密码都不填直接提交的话,我们拦截器添加的动作就会检测到这种行为,并作出判断,告诉浏览器,此时应该跳转到一个页面显示一些提示信息,比如这个页面是appleyk.jsp,如果用户名和密码都有但不符合要求,我们让它跳转到一个失败界面Error.jsp,提示登录失败,好了,先把上面提到的jsp页面一一实现:

a.Login.jsp:


b.Welcome.jsp(不截图了,直接贴代码,注意创建的jsp页面的位置)

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@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=UTF-8"><title>登录成功页面</title></head><body>    ${username}你好,你已经登录! </body></html>

c.appleyk.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>验证是否拦截到这个页面</title></head><body>   没错,还真跳转到了这个界面,但是请输入你的用户名和密码!</body></html>

d.Error.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>登录失败页面</title></head><body>  对不起,登录失败!</body></html>

(2)页面添加完了,下面我们要处理动作(拦截处理)了,添加LoginAction类继承ActionSupport

点击Finish,然后我们对LoginAction类进行一些处理,首先需要有getter和setter,分别针对username和password处理,当然,写代码的时候,你会有疑问,这样写,代码会被调用嘛?这个你就不用管了,每个action都是新开的一个对象,所以首先不存在线程安全问题,每个action都会有一个struts.xml配置文件里对应的配置信息,而这个配置信息,就是告诉这个action对应的处理对象,要实现哪些setter和getter,而具体操作需要重写execute()方法来实现,这个方法里面你就可以过滤掉一些"信息"。

Login.java:

package com.appleyk.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {/** * 类版本号 */private static final long serialVersionUID = 1L;private String username;private String password;public String getUsername(){return username;}public void setUsername(String username){this.username=username;}public String getPassword(){return password;}public void setPassword(String password){this.password=password;}@Overridepublic String execute() throws Exception {if(getUsername().equals("appleyk") && getPassword().equals("521")){//ActionContext.getContext().getSession().put("user", getUsername());return "success";}else if(getUsername().equals("") & getPassword().equals("")){return "welcome";}elsereturn "error";}}


B:接着,第三步,也是最后一步,在src文件夹下创建一个struts.xml,并配置


内容如下:

struts.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd "><struts><package name="test"  extends="struts-default"><action name="login" class="com.appleyk.action.LoginAction"><!-- 定义三个逻辑视图和物理资源之间的映射 --><!-- 在class中写:如果用户名和密码都为空的话,返回welcome,跳转到appleyk.jsp --><result name="welcome">/appleyk.jsp</result><!-- 在class中写:如果用户名和密码等于规定的值的话,返回success,跳转到Welcome.jsp --><result name="success">/Welcome.jsp</result><!-- 在class中写:如果用户名和密码不等于规定的值的话,返回error,跳转到Error.jsp --><result name="error">/Error.jsp</result></action></package></struts>

如果不清楚struts.xml配置文件为什么要这么写,可以参考最开始下载的struts-2.3.33文件夹里面的struts.xml,这里我不细说了,直接上图,你们可以自行查看:


ABC三步搞定后,就可以重新启动下我们的Tomcat 服务,看一下项目中在加了struts2框架后,会不会报错?

信息: Starting Servlet Engine: Apache Tomcat/8.5.16  (注:这里提示tomcat正常启动)
七月 28, 2017 5:14:32 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
七月 28, 2017 5:14:33 下午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
警告: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [226] milliseconds.
七月 28, 2017 5:14:33 下午 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider info
信息: Parsing configuration file [struts-default.xml]
七月 28, 2017 5:14:33 下午 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
七月 28, 2017 5:14:33 下午 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider info
信息: Parsing configuration file [struts-plugin.xml]
七月 28, 2017 5:14:33 下午 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider info
信息: Parsing configuration file [struts.xml] (注:这里提示我们配置的struts.xml也加载正常)
七月 28, 2017 5:14:33 下午 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.ObjectFactory)
七月 28, 2017 5:14:33 下午 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.factory.ActionFactory)
七月 28, 2017 5:14:33 下午 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.factory.ResultFactory)
七月 28, 2017 5:14:33 下午 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.factory.ConverterFactory)
七月 28, 2017 5:14:33 下午 org.apache.struts2.config.AbstractBeanSelectionProvider info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.factory.InterceptorFactory)

上面只是拷贝了部分启动日志,但是日志告诉我们,tomact8.5+struts2.3+MyWebTest 的模式已经在正常工作了

插一句:我在看到这个日志之前,经历了好一番折腾,启动日志老是提示我过滤器找不到,struts.xml死活加载不了,后来发现,配置文件一切正常话,那就是所导入的jar包缺少了,最后发现,果然是少了一个jar包引起的,少一个jar包,struts2就歇菜了,再一次贴上本例子所需要引入的几个关键包:


最后演示效果:

Login.jsp,当输入appleyk,521提交时:


当输出错误用户名和密码时:

如果什么都不输入的话呢?


ok,再让我们回头看下,我们struts.xml中写的,就会发现,这些页面并不是随便跳转的,而是可以通过配置文件来控制这种登录的行为,是不是感觉有点IOC控制反转的意思。