一、首先使用MyEclipse新建一个web项目,步骤如下:
1.File →New→Web Project命令,弹出相应对话框,在Project name中输入你的项目名称
选择我们自己安装配置的版本<Java version>(我的是1.7版本的),在Targer runname选
择相应的Tomcat ,如图所示,点击下一next步:
2.进入Java界面,此处可以不用修改,点击next:
3.进入Web Module界面在其中勾选Generate web.xml......复选框,点击next:
4点击finish:
5.创建完成之后的StrutsDemo,如图所示:
二、添加Struts2框架的支架文件。
在lib文件夹中添加接下来我们需要的包,如图:
三、新建JSP(Hello.jsp)页面,并添加相关代码,如图:
</head><body> <center> <!-- hello是请求的action,也可以写成hello.action --> <h3><a href="hello">从Hello.jsp页面跳转到HelloWord.jsp页面</a></h3> </center></body>
此处应选择“utf-8”
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
在新建一个HelloWorld.jsp页面,在其中显示欢迎的内容:
</head> <center> <h2>欢迎来到struts 2的世界!</h2> <h3>第一个程序,hello world</h3> </center>
四.在web.xml中添加过滤器,如:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>StrutsDemo</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>/*</url-pattern> </filter-mapping> </web-app>
五.创建业务控制器HelloAction类(让HelloAction类继承com.opensymphony.xwork2包中的ActionSupport类)
HelloAction类的代码如下:
package com.yzpc.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
@Override
public String execute() throws Exception{
return SUCCESS;
}
}
六.编写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> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.yzpc.action.HelloAction"> <result name="success">/HelloWorld.jsp </result> </action> </package> </struts>
五、六文件夹所在位置
七.启动Tomcat 7x后,在浏览器的地址栏中输入:http://localhost:8080/StrutsDemo/Hello.jsp.
在启动Tomact的时候可能会出现此类现象(这是我个人在运行时出现的问题),意思是缺少项目运行时
所需要的包;之后在lib中添加缺少的包即可。
如果还是不行得话,要检查一下web.xml文件下的<filter-class>内的内容要写在一行内
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
严重: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter .StrutsPrepareAndExecuteFilter at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1898) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) 七月 27, 2017 8:08:37 下午 org.apache.catalina.core.StandardContext startInternal 严重: Error filterStart
运行结果:
点击上面的字体连接到: