1.引入struts2相应的jar包,可以到网上下载,大把大把的。
2.配置struts.xml 要放在src目录下 如下:
Java代码
- <?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>
- <!-- Struts 2 的Action必须放在包空间下 -->
- <package name="strutsqs" extends="struts-default">
- <!-- 定义action的名字以及action的实现类 -->
- <action name="LoginAciton" class="com.gjy.struts2.loginAction.LoginAction">
- <!-- 定义action的处理结果result,result有两个属性,其中name指定返回名称,tyle指定返回的类型 -->
- <!-- 处理Login错误时返回/error.jsp -->
- <result name="error">/error.jsp</result>
- <!-- 处理Lognin正确时/suc.jsp -->
- <result name="suc">/suc.jsp</result>
- </action>
- </package>
- </struts>
3.配置web.xml struts2与struts1不同,struts2是通过一系列的过滤器来组成过滤链来对用户的请求作处理
如下:
Java代码
- <?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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>struts2</filter-name>
- <!--定义struts2的核心Filter的实现类 -->
- <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>