《精通struts2实用教程》学习笔记-01

开篇说明:

本系列的笔记是根据《精通struts2实用教程》而来。

好了,开始今天的《精通struts2实用教程》学习笔记-01。(因为第一章是介绍struts2的来源,并没有涉及到实际操作,故从第二章开始。)

开发工具为Eclipse (Version: Luna Release (4.4.0))


1、下载struts2

开发前,需要下载struts2: 25页中有提到下载网址:http://struts.apache.org/download.cgi#Struts206 现在的struts2版本已经到了2.3.16.3了;或者点这个直接下载

2、创建第一个简单struts工程


3、修改WEB-INFO下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
<span style="white-space:pre">	</span>xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre">	</span>xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<span style="white-space:pre">	</span><!-- <display-name>Struts Blank</display-name> -->


<span style="white-space:pre">	</span><filter>
<span style="white-space:pre">		</span><filter-name>struts2</filter-name>
<span style="white-space:pre">		</span><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<span style="white-space:pre">	</span></filter>


<span style="white-space:pre">	</span><filter-mapping>
<span style="white-space:pre">		</span><filter-name>struts2</filter-name>
<span style="white-space:pre">		</span><url-pattern>/*</url-pattern>
<span style="white-space:pre">	</span></filter-mapping>


<span style="white-space:pre">	</span><welcome-file-list>
<span style="white-space:pre">		</span><welcome-file>login.jsp</welcome-file>
<span style="white-space:pre">	</span></welcome-file-list>


</web-app>

4、往lib添加jar包



5、应用到工程中。




6、创建类LoginAction

package struts2simple.login;

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(){
		if (getUsername().equals("scott") && getPassword().equals("tiger")) {
			return "success";
		} else {
			return "error";
		}
	}
}

7、在目录src中添加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>

	<package name="struts2simple" extends="struts-default">
		<action name="Login" class="struts2simple.login.LoginAction">
			<result name="error">WEB-INF/error.jsp</result>
			<result name="success">WEB-INF/welcome.jsp</result>
		</action>
	</package>

</struts>

8、在WebContent中添加login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
	<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>


【有可能有错误提示 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 可按以下步骤解决】




9、在WEB-INF添加welcome.jsp error.jsp

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
成功登陆
</body>
</html>


error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
登陆错误
</body>
</html>

10、运行测试:

http://localhost:8080/struts2simple/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值