Struts2 HelloWorld

Struts2 HelloWorld

 

1、开发环境(Eclipse3.4 ,tomcat5.5,jdk1.5)

2、jar包(见附件lib)

3、web.xml

    

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>TestStruts2</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.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>
</web-app>

 

4、struts.xml

    

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="WebContent" extends="struts-default">
		<action name="Login" class="action.LoginAction">
		 <result name="error">/error.jsp</result>
		 <result name="success">/welcome.jsp</result>        
		</action>
	</package>
</struts>

 

5、action

   

package action;

public class LoginAction 
{
	private String username;
	private String password;
	
    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
    {
		 if(getUsername().equals("scott") && getPassword().equals("tiger") )
		 {
			 return "success";
		 }
		 else
		 {
			 return "error";
		 }
    }


	
}

 

6、jsp页面

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Login.action" method="post">
     <table align="center">
     <caption><h3>用户登录</h3></caption>
     <tr>
      <td>用户名:<input type="text" name="username"/></td>
     </tr>
     <tr>
      <td>密&nbsp;&nbsp;码:<input type="text" name="password"/></td>
     </tr>
     <tr align="center">
      <td colspan="2"><input type="submit" value="登录"/>
      <input type="reset" value="重填" /></td>
     </tr>
     </table>
</form>
</body>
</html>

  

7、注意:tomcat5.5中common下endorsed文件夹下的两个jar包,需要替换一下,tomcat5.5原来带的不是很好用,使用HelloWorld Demo 中lib下的就可以用。

8、JUnit Test

package action;
import static org.junit.Assert.*;

import org.junit.Test;

import com.opensymphony.xwork2.ActionSupport;
public class LoginActionTest {
	@Test
	public void testExecute() 
	{
		LoginAction hello = new LoginAction();
        hello.setUsername("scott");
        hello.setPassword("tiger");
        String result;
		try 
		{
			result = hello.execute();
			assertTrue("Expected a success result!", ActionSupport.SUCCESS.equals(result));
	        final String msg = "scott";
	        assertTrue("Expected the default username!", msg.equals(hello.getUsername()));
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(Web,jsp,xml,struts,JUnit)