1.添加struts2的jar包,建议使用maven,方便又能看源码,美滋滋
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.1</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency>2.在web.xml中添加过滤器struts2
<!-- 配置Struts2 核心 Filter --> <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意:不同版本的struts2使用的filter-class不同,网上也是千奇百怪的,不明白的可以打开该类看其是否实现Filter接口,以确认要使用的filter-class是否正确。
3.创建Action,以达到struts2能够控制jsp页面如何跳转的功能。
package com.struts2.Action; import com.opensymphony.xwork2.Action; public class LoginAction implements Action{ private String uname; //表单中的姓名 private String upass; //表单中的密码 //执行方法 public String execute() throws Exception { System.out.println("姓名:"+uname); System.out.println("密码:"+upass); if (uname.equals("leaf") && upass.equals("leaf")) { return "success"; } return "error"; } //记住, 要有get和set方法 public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } }
上述Action类对应的struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.struts2.Action.LoginAction"> <result name="success">/struts2/welcome.jsp</result> <result name="error">/struts2/fail.jsp</result> </action> </package> <!-- Add packages here --> </struts>注意:这里可以实现Action接口,也可以不实现Action接口,实现的话,不需要在struts2.xml中进行配置,struts2默认会自动调用该类的execute()方法,不实现的话需要在struts2.xml指定方法,上面的LoginAction和struts.xml是一套配置,下面的是另外一套配置,其中上面实现了Action接口,下面未实现该接口!
package com.struts2.Action; public class LoginAction2 { private String uname; // 表单中的姓名 private String upass; // 表单中的密码 // 执行方法 public String action2_Execute() throws Exception { System.out.println("action2_Execute 姓名:" + uname); System.out.println("action2_Execute 密码:" + upass); if (uname.equals("leaf") && upass.equals("leaf")) { return "success"; } return "error"; } // 记住, 要有get和set方法 public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="default" namespace="/" extends="struts-default"> <action name="login2" class="com.struts2.Action.LoginAction2" method="action2_Execute"> <result name="success">/struts2/welcome.jsp</result> <result name="error">/struts2/fail.jsp</result> </action> </package> </struts>注意:还需要将index.jsp中所要提交的action地址给换一下.
4.创建index.jsp、fail.jsp、success.jsp页面。
index.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>Insert title here</title> </head> <body> <form action="login"> 用户名:<input type="text" name="uname"><br> 密 码:<input type="text" name="upass"><br> <input type="submit" value="登陆" /> </form> </body> </html>
fail.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>fail page</title> </head> <body> 错误 </body> </html>
welcome.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>My JSP 'welcome.jsp' starting page</title> </head> <body> welcome struts2 </body> </html>
上述代码运行成功后,页面的跳转没有改变,但是控制台打印的内容是不一样的。
下图是这个Demo的配置图,非标记内容和本demo无关,请忽略: