Struts2练习
首先从下载的Struts2中apps文件夹下选中需要联系的示例,拷贝jar包到eclipse项目WEB-INF/lib目录下,然后构建路径
二、在web.xml添加过滤器 并复制配置文件 struts.xml 到src目录下,
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Test24</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- struts2的过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
三、根据需要需改struts.xml
<?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> <!-- 修改默认配置 --> <constant name="struts.action.extension" value="do,action,,"></constant> <!-- 对功能模块分组管理 --> <package name="index" extends="struts-default" > <action name="login" class="com.hanqi.action.TestAction" method="checkLogin"> <result type="dispatcher">/WEB-INF/pages/main.jsp</result> <!-- <result name ="fail" type="redirect">http"//www.baidu.com</result> --> <!-- <result name ="fail" type="redirectAction"> <param name="actionName">test1</param> </result>--> <result name ="fail" type="chain"> <param name="actionName">test2</param> <param name="method">chain</param> </result> </action> <!-- 被重定向到的action --> <action name="test1"> <result>/WEB-INF/pages/test1.jsp</result> </action> <action name="test2"> <result>/WEB-INF/pages/test2.jsp</result> </action> </package> </struts>
四、首页
<%@ 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" method="post"> 用户:<input type="text" name="userID"> <br> <br> 密码:<input type="password" name="password"> <br> <br> <input type="submit" value="登录"> </form> </body> </html>
五、Java类判断条件
package com.hanqi.action; public class TestAction { private String userID; private String password; public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String checkLogin() { String rtn = "fail"; if(userID!=null&&password!=null&&userID.equals("admin")&&password.equals("123456")) { rtn="success"; } return rtn; } public String execute() { return "success"; } public String chain() { return "success"; } }
六、在WEB-INF目录下建立pages文件夹,然后新建几个jsp文件,实现Struts2的过滤跳转功能
现在通过Struts2框架基本已经实现了一个基本的过滤跳转功能,结果如下: