这是一个验证表单中填入的用户名和密码值并进行跳转和现实内容的应用。
步骤:
1.新建项目"struts2_2"并引入jar包;
2.在src目录下新建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> </struts>
3.新建login.jsp文件,用于用户的登录:
4.在src/books目录下新建Action接口:<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>信息</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> </head> <body> <s:form action="GetBooks.action"> <s:textfield name="username" label="用户名"/> <s:password name="password" label="密码" /> <s:submit value="提交" /> </s:form> </body> </html>
5.新建存储数据的BooksService.javapackage books; public interface Action { public static String SUCCESS = "success"; public static String NONE = "none"; public static String ERROR = "error"; public static String INPUT = "input"; public static String LOGIN = "login"; public String execute() throws Exception; }
package books; public class BooksService { private String[] books = new String[]{"book1","book2","book3","book4"}; //获取书本 public String[] getLeeBooks() { return books; } }
6.新建GetBooksAction.java类,用来验证登录并转入书籍列表:
package books; import com.opensymphony.xwork2.ActionContext; public class GetBooksAction implements Action{ private String[] books; //图书信息数据 private String username; //用户名 private String password; //密码 public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { //验证用户登录的用户名和密码是否正确 if (getUsername().equals("admin") && getPassword().equals("admin")) { //用户名正确,将用户名放到ActionContext的Session中"user"参数 ActionContext.getContext().getSession().put("user", getUsername()); } else { //用户名不正确,将用户名放到ActionContext的Session中"user"参数 ActionContext.getContext().getSession().put("user", "error"); } //从Session中取出"user"参数 String user = (String)ActionContext.getContext().getSession().get("user"); System.out.println(user); //判断"user"是否equalse("admin") if (user != null && user.equals("admin")) { //登录成功了,创建BooksService业务类,调用bs的getLeeBooks()方法获取图书信息、 BooksService bs = new BooksService(); setBooks(bs.getLeeBooks()); //设置Action的books return SUCCESS; } else { //否则重新登录 return LOGIN; } } }
7.在刚才新建好的额struts.xml中配置Action
8.新建用于显示书籍的showBooks.jsp<?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> <include file="struts-default.xml"></include> <package name="Struts2_2" extends="struts-default"> <action name="GetBooks" class="books.GetBooksAction"> <result name="login">/login.jsp</result> <result name="success">/showBooks.jsp</result> </action> </package> </struts>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <%@ page import="java.util.*,com.opensymphony.xwork2.util.*" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>信息</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> </head> <body> <s:iterator value="books" status="index"><!-- 使用struts2迭代标签,迭代books数组 --> <s:if test="#index.odd==true"><!-- 使用struts判断标签 --> <span> <s:property/> </span><br> </s:if> <s:else> <s:property/> </s:else> </s:iterator> </body> </html>
9.修改web.xml,添加如下代码:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
至此,一个简单的练习就完成了。运行效果如下:
登录界面(登录失败继续跳转到该页面):
登录成功后的界面: