注册这个账号也挺久了,一直在看别人写的文章,学习别人的学习经验,最近也想写点东西记录一下自己的学习过程和学习中遇到的问题以及最后的解决方法。
最近在学struts2,那就先写第一个struts2的配置过程吧。
一.先引入struts2的几个核心jar包
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
二.配置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>
</filter-mapping>
</web-app>
Filter-name是自己起的名字,filter-class就是核心过滤器,是struts2-core-2.3.4.1.jar
中org.apache.struts2.dispatcher.ng.filter包下的StrutsPrepareAndExecuteFilter。
三.在src目录下建一个包,写一个HelloAction的类(我的包为com.strust.action)
我的第一个测试方法
public Stringexecute() throws Exception {
System.out.println("Hellostruts2");
return"success";
}
四.最后一步就是配置struts.xml了
在src目录下建一个struts.xml文件
<struts>
<package name="day1" extends="struts-default"namespace="/">
<action name="hello"class="com.struts.action.HelloAction " method="execute">
<resultname="success">/index.jsp</result>
</action>
</package>
</struts>
接下来就是测试了,在jsp页面写<a href=”hello”>hellostruts</a>
如果在网页上点击链接跳转到index.jsp页面就成功了!