一.导包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.5.13</version>
</dependency>
二.配置struts.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 5 "http://struts.apache.org/dtds/struts-2.5.dtd"> 6 7 <struts> 8 <constant name="struts.devMode" value="true"/> 9 <constant name="struts.serve.static.browserCache" value="false"/> 10 <!--支持动态调用--> 11 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 12 <!--在浏览器上访问的路径 == namespace + name --> 13 <package name="default" extends="struts-default,json-default" namespace="/"> 14 <action name="register" class="com.yztc.demo.action.AccountAction" method="register"> 15 <result name="success">/test.jsp</result> 16 <allowed-methods>register</allowed-methods> 17 </action> 18 <action name="login" class="com.yztc.demo.action.AccountAction" method="login"> 19 <result name="success">/test.jsp</result> 20 <allowed-methods>login</allowed-methods> 21 </action> 22 <action name="update" class="com.yztc.demo.action.AccountAction" method="update"> 23 <result name="success">/test.jsp</result> 24 <allowed-methods>update</allowed-methods> 25 </action> 26 </package> 27 </struts>
三.配置web.xml配置
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 8 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 9 version="3.1"> 10 <display-name>Archetype Created Web Application</display-name> 11 <filter> 12 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 13 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 14 </filter> 15 <filter-mapping> 16 <filter-name>StrutsPrepareAndExecuteFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 </web-app>
四.创建class继承ActionSupport,如下:
1 package com.yztc.demo.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 /** 6 * _ooOoo_ 7 * o8888888o 8 * 88" . "88 9 * (| -_- |) 10 * O = /O 11 * ___/`---'\____ 12 * . ' \| |// `. 13 * / \||| : |||// 14 * / _||||| -:- |||||- 15 * | | \ - /// | | 16 * | \_| ''---/'' | | 17 * .-\__ `-` ___/-. / 18 * ___`. .' /--.-- `. . __ 19 * ."" '< `.___\_<|>_/___.' >'"". 20 * | | : `- \`.;` _ /`;.`/ - ` : | | 21 * `-. \_ __ /__ _/ .-` / / 22 * ======`-.____`-.___\_____/___.-`____.-'====== 23 * `=---=' 24 * ............................................. 25 * 26 * @author bindu 27 * @date 2017-10-24 14:59 28 */ 29 30 31 public class AccountAction extends ActionSupport { 32 public String register(){ 33 return SUCCESS; 34 35 } 36 public String login(){ 37 return SUCCESS; 38 39 } 40 public String update(){ 41 return SUCCESS; 42 43 } 44 }
五.配置test.jsp 文件:
1 <%-- 2 Created by IntelliJ IDEA. 3 User: bindu 4 Date: 2017/10/24 5 Time: 15:21 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>Title</title> 12 </head> 13 <body> 14 测试代码 15 </body> 16 </html>