1、web.xml中增加Struts2配置
<filter> <filter-name>struts2</filter-name> <filter-class> <!--org.apache.struts2.dispatcher.FilterDispatcher不推荐使用--> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2、在WEB-INF/lib中加入jar
struts2-core-2.x.x.jar、xwork-core-2.x.x.jar、log4j-1.x.x.jar、commons-fileupload-1.x.x.jar、commons-lang-2.x.jar、commons-logging-1.x.x.jar、freemarker-2.x.x.jar、javassist-3.x.GA.jar
3、配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.devMode" value="true"/> <constant name="struts.action.extension" value="do,action"/> <constant name="struts.locale" value="zh_CN" /> <package name="struts" extends="struts-default"> <action name="hello" class="com.example.demo.action.HelloAction"> <result name="success">/hello.jsp</result> </action> </package> </struts>
4、编写action
package com.example.demo.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport{ private static final long serialVersionUID = -4395197537614656411L; private String message; public String execute() { message = "xiao hei!"; return SUCCESS; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
5、编写resultpath-hello.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>welcome</title> </head> <body> Hello world, ${message} </body> </html>
6、启动tomcat,浏览器输入http://localhost/strutsdemo/hello.do
Hello world, xiao hei!
附项目:http://pan.baidu.com/share/link?shareid=3806371118&uk=1426082551