首先添加一个login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="GetBooks.action">
<s:textfield name="username" label="账号"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="递交"></s:submit>
</s:form>
</body>
</html>
让后页面跳转action,当然是通过struts.xml,但是在这个之前要先配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>StrutsTutorial</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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>
<!-- 引入默认文件(可能就是以前这里没有引入,所以错误啦) -->
<include file="struts-default.xml"></include>
<package name="login" extends="struts-default">
<action name="GetBooks" class="books.GetBooksAction">
<result name="login">/login.jsp</result>
<result name="success">/showBooks.jsp</result>
</action>
</package>
</struts>
跳转到的当然是三个java
package 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[]{"郭德纲相声选","郭德纲相声全","曹云金相声选","曹云金相声全"};
public String[] getLeeBooks()
{
return books;
}
}
package books;
public class BooksService
{
private String[] books=new String[]{"郭德纲相声选","郭德纲相声全","曹云金相声选","曹云金相声全"};
public String[] getLeeBooks()
{
return books;
}
}
package books;
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;
}
public String execute() throws Exception
{
// TODO Auto-generated method stub
BooksService bs=new BooksService();
setBooks(bs.getLeeBooks());
return SUCCESS;
}
}
最后当然是输出结果的页面
<%@ page language="java" import="java.util.*" contentType="text/html;charset=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>
</head>
<body>
<s:iterator value="books" status="index">
<s:if test="#index.odd==true">
<span style="color:#ff0000"><s:property/></span><br/>
</s:if>
<s:else>
<s:property/><br/>
</s:else>
</s:iterator>
</body>
</html>