首先,开始登录:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page isELIgnored="false"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>登陆</title> </head> <body> <s:form method="post" action="login.action"> <p> <s:textfield type="text" name="loginInfo.uesrname" label="用户名" /> <br> <s:textfield type="text" name="loginInfo.password" label="密码" /> <br> <br> <s:submit value="提交" /> </p> </s:form> </body> </html>
发送的actin为login.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> <constant name="struts.devMode" value="true" /> <package name="lyyStruts1" extends="struts-default"> <action name="login" class="controller.Login"> <result name="list" type="redirectAction"> <param name="actionName"> List </param> </result> </action> <!-- 小写不行 --> <action name="List" class="controller.List"> <result name="bookListJsp"> /bookList.jsp </result> </action> </package> </struts>
action name就是提交的action去掉action后缀。实现类是controller.Login。然后重定向到list action中。再通过list.action显示。
Login.java
public class Login { public String execute() throws Exception { return "list"; } }
List.java
public class List { private ArrayList bookList = new ArrayList(); public ArrayList getBookList() { return bookList; } public void setBookList(ArrayList bookList) { this.bookList = bookList; } public String execute() { bookList.add("book1"); bookList.add("book2"); bookList.add("book3"); bookList.add("book4"); return "bookListJsp"; } }
最后,显示结果的页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page isELIgnored="false"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!-- --> <!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> <s:iterator var="bookName" value="bookList"> <s:property value="bookName" /> </s:iterator> <!--不支持jstl标签库 <c:forEach var="bookName" items="${bookList}"> <c:out value="${bookName}"></c:out> </c:forEach> --> </body> </html>