下载安装好structs2
1.创建并配置好web应用程序,使支持structs2:
将structs2/lib下的jar文件除了Junit,sprint-test.jar都拷贝到web-inf/lib下面
2.配置web应用的web.xml文件。拦截所有url,使用struct2
<?xml version="1.0" encoding="GBK"?> <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"> <!-- 定义Struts2的核心Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 让Struts2的核心Filter拦截所有请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.创建一个hello.jsp ,在里面输入helloworld!
4.在src目录下创建一个structs.xml,配置struts下跳转到hello.jsp
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.1.7.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"> <!-- 指定Struts 2配置文件的根元素 --> -<struts> <!-- 指定全局国际化资源文件 --> <constant value="mess" name="struts.custom.i18n.resources"/> <!-- 指定国际化编码所使用的字符集 --> <constant value="GBK" name="struts.i18n.encoding"/> <!-- 所有的Action定义都应该放在package下 --> <package name="default" extends="struts-default"> <action name="hello" class=""> <!-- 定义逻辑视图和物理资源之间的映射 --> <result name="input">/hello.jsp</result> </action> </package> </struts>
好了,这样在浏览器中输入:localhost:8080/web项目名称/hello
就可以看到helloworld!了。