Create a project strutsdemo1 in eclipse. [For struts configuration, see struts development. <configure struts in eclipse. 1>
The directory structure is as follows:
Method 1
Create useraction
package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}
Reconfigure in struts
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.3 // en" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name = "default" namespace = "/" extends = "struts-Default"> <! -- First, configure multiple actions --> <action name = "add" class = "fzl. user. struts. demo. useraction "method =" add "> <result name =" success ">/WEB-INF/user/Add. JSP </result> </Action> <action name = "input" class = "fzl. user. struts. demo. useraction "method =" input "> <result name =" success ">/WEB-INF/user/input. JSP </result> </Action> <action name = "list" class = "fzl. user. struts. demo. useraction "method =" list "> <result name =" success ">/WEB-INF/user/list. JSP </result> </Action> </package> </struts>
Create input. jsp, list. jsp, and Add. 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">
<%@ 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">
<%@ 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">
Run Tomcat and enter http: // localhost: 9000/strustdemo1/input in the browser.
Similarly, input the response directory to get the corresponding page output.
Method 2
You can directly change useraction and Struts. xml without any changes.
package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "list";}public String input(){System.out.println("input");return "input";}public String add(){System.out.println("add");return "add";}}
<action name="User" class="fzl.struts.demo.UserAction"> <result name="add">/WEB-INF/User/list.jsp</result> <result name="input">/WEB-INF/User/input.jsp</result> <result name="list">/WEB-INF/User/add.jsp</result> </action>
Enter http: // localhost: 9000/strutsdemo2/user! Add to get the corresponding page
Method 3
package fzl.user.struts.demo;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {public String list(){System.out.println("list");return "success";}public String input(){System.out.println("input");return "success";}public String add(){System.out.println("add");return "success";}}
If you are not familiar with the wildcard configuration
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="fzl.user.struts.demo.{1}Action"method="{2}"><result>/WEB-INF/{1}/{2}.jsp</result></action> </package></struts>
There are three methods to configure actions. The last two methods are usually used in development.
Struts development <detailed configuration of actions in struts. 2>