您的位置:首页 > 编程语言 > Java开发

深入浅出学习struts框架(五)--正式进入struts框架学习,从一个实例开始 .

2012-03-24 21:40 671 查看
之前写了四篇博客都是struts框架学习之前的引子,主要就是从mvc的一个实例开始,慢慢重构出一个struts框架雏形,通过这个雏形来引出我们要学习的struts框架。四篇博客如下:

1、《深入浅出学习Struts框架(一):一个简单mvc模式代码示例开始

2、《深入浅出学习Struts框架(二):重构MVC模式代码中跳转路径和业务逻辑

3、 《深入浅出学习Struts框架(三):彻底去掉TestServlet中的字符串和if-else语句块》

4、《深入浅出学习struts框架(四):从MVC模式代码认识struts框架

深入浅出学习struts框架(四):从MVC模式代码认识struts框架 博客中解释了框架是什么以及struts框架的结构,通过上篇博客我们明白了框架无非就是一个应用程序的半成品,是一系列的高复用的无关业务的组件;并且通过结构图我们很明显的看到struts是一个基于mvc的框架。



这篇博客通过一个struts实例来看struts框架,先来学会struts框架是怎样使用的,随后的博客在进一步分析它的实现代码和实现原理。

在写struts框架实例之前,希望读者能够看一下前面的四篇博客的实例代码,因为那代码是struts框架的雏形,只要那四篇的代码看懂,那么下面的struts框架代码也是非常容易看懂的。

Struts框架实例—登录实例:

1、实例开始工作—导入jar包,在官网上下载struts框架包,解压之后导入工程的:



2、之后配置web.xml(这里的具体配置方法可以参见struts框架包中的实例文件夹webapps中的实例代码中web.xml文件的配置方法):



具体如下:

[html]
view plaincopyprint?

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app 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"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app></span>

[html]
view plaincopyprint?

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"></br>
<input type="submit" value="登录">
</form>
</body>
</html></span>

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"></br>
<input type="submit" value="登录">
</form>
</body>
</html></span>


切记那个action后面的路径一定要是.do开头的,因为我们在web.xml中配置的是*.do。这里依旧不介绍为什么随后博客会深入分析。

4、建立两个异常类,一个是用户名未找到、一个是密码错误:

①用户名未找到

[java]
view plaincopyprint?

<span style="font-size:18px;">public class UserNotFoundException extends RuntimeException {

public UserNotFoundException() {
// TODO Auto-generated constructor stub

}

public UserNotFoundException(String message) {
super(message);
// TODO Auto-generated constructor stub

}

public UserNotFoundException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub

}

public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub

}

}</span>

[java]
view plaincopyprint?

<span style="font-size:18px;">public class PasswordErrorException extends RuntimeException {

public PasswordErrorException() {
// TODO Auto-generated constructor stub

}

public PasswordErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub

}

public PasswordErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub

}

public PasswordErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub

}

}</span>

<span style="font-size:18px;">public class PasswordErrorException extends RuntimeException {

public PasswordErrorException() {
// TODO Auto-generated constructor stub
}

public PasswordErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public PasswordErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

public PasswordErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

}</span>


5、业务处理类代码:

[java]
view plaincopyprint?

<span style="font-size:18px;">public class UserManager {

public void login(String username, String password) {
if (!"admin".equals(username)) {
throw new UserNotFoundException();
}

if (!"admin".equals(password)) {
throw new PasswordErrorException();
}

}
}</span>

[java]
view plaincopyprint?

<span style="font-size:18px;">import org.apache.struts.action.ActionForm;

/**
* 登录ActionForm,负责表单收集数据
* 表单的属性必须和ActionForm中的get和set的属性一致
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {

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;
}

}</span>

<span style="font-size:18px;">import org.apache.struts.action.ActionForm;

/**
* 登录ActionForm,负责表单收集数据
* 表单的属性必须和ActionForm中的get和set的属性一致
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {

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;
}

}</span>


7、LoginAction类的建立,这个是负责取得表单数据、调用业务逻辑以及返回转向信息。

[java]
view plaincopyprint?

<span style="font-size:18px;">import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* 登录Action
* 负责取得表单数据、调用业务逻辑、返回转向信息
*
* @author Administrator
*
*/
public class LoginAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

LoginActionForm laf=(LoginActionForm)form;
String username=laf.getUsername();
String password=laf.getPassword();
UserManager userManager=new UserManager();
try{
userManager.login(username, password);
return mapping.findForward("success");
}catch(UserNotFoundException e){
e.printStackTrace();
request.setAttribute("msg", "用户名不能找到,用户名称=["+username+"]");
}catch(PasswordErrorException e){
e.printStackTrace();
request.setAttribute("msg", "密码错误");
}
return mapping.findForward("error");

}
}</span>

[html]
view plaincopyprint?

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
${loginForm.username },登录成功
</body>
</html></span>

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
${loginForm.username },登录成功
</body>
</html></span>


②登录失败页面

[html]
view plaincopyprint?

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<%--
<%=request.getAttribute("msg") %>
--%>
${msg }
</body>
</html></span>

[html]
view plaincopyprint?

<span style="font-size:18px;"><?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
</form-beans>

<action-mappings>
<action path="/login"
type="com.bjpowernode.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp" />
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config></span>

<span style="font-size:18px;"><?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
</form-beans>

<action-mappings>
<action path="/login"
type="com.bjpowernode.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp" />
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config></span>


经过配置之后实例就已经做完了,感兴趣童鞋可以自己手动运行一下。

这篇博客仅仅是提供了一个struts框架的简单实例,具体分析还希望等下面的博客。在下面的博客中就要对struts框架进行分析了,我们会看到我们在原先博客mvc实例重构中出现的截取字符串、mapping、读取配置文件的操作是如何在struts框架中实现的,struts框架是怎样运行的,它都给我们做了什么等等。敬请期待!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: