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>密 码:<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();
}
}
}
分享到:
相关推荐
Struts2 Helloworld 源代码
struts2框架最基本的代码。如果你想看看struts2最基本的样子,你个应该是你不错的选择。但前提是你得学过jsp/servlet
struts2HelloWorld 简单的实用例子!
struts2 入门示例 HellWorld 工程源码。
一个完整的struts2框架实现实例,需要的包、配置文件均可参考。
Struts2的入门小实例helloworld,jar包齐全,可以运行。
纠结了好久后解决掉了struts2 的helloWorld程序一系列问题,步骤以及一些错误等在我的博文里。
Struts2做的一个HelloWorld示例,使用Maven构建,导入请前先安装maven。
这个是struts2入门示例HelloWorld所需的最小必备包。
自己编写的一个helloworld. 使用struts2.0.11.2, 在eclipse3.4, tomcat5.5, jdk1.5平台上写的代码. 没有使用jarkatar提供的空白模板struts2-blank-2.0.11.2.war(发现这个模板有问题). 已集成到war文件中, 可以使用...
NULL 博文链接:https://plane.iteye.com/blog/453686
这是一个Struts2的例子,MyEclipse运行
完整项目 包 博文链接:https://huibuqule.iteye.com/blog/1882869
最基础的基于struts2框架的hello world 实现,适合小白。
学Struts2从HelloWorld示例开始
自己做的 Struts2.1.6 HelloWorld 小例子
可运行的struts2例子,eclipse工程