第二个成功的struts(不断的弹出各种错误,于是我卸掉tomcat和eclipse重装,删除以前的软件ok)(中间建立了一个工程还是会弹出错误,让后我就重做工程)(很多知识点需要温习)


首先添加一个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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值