Struts2初级教程02 登陆demo

一.新老框架简单对比

Web项目就会遇到对用户输入数据的接收,传统web应用程序是于开发人员调用HttpServletRequest.getParameter(String name)方法从请求中取得数据.而web框架都提供了数据绑定机制,由框架从请求中猎取数据然后以一个JavaBean对象中.Struts2的action完全与web层解耦,要获取web层的数据,需要使用ActionContext,它为action提供了一个执行上下文.


二.登陆demo

新建项目,导入包这样的操作在前面的教程中都有...
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="loginAction.action" method="post">
		<table>
			<tr>
				<td>name:</td>
				<td><input name="user.name" /></td>
			</tr>
			<tr>
				<td>password:</td>
				<td><input name="user.password" type="password"/></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="登陆" /></td>
			</tr>
		</table>
	</form>
</body>
</html>


你会看到这样的代码
<input name="user.password" />
通过Struts2框架的数据绑定机制,传递的user.name请求参数等同于调用
action.getUser().setName(...);
但是我们注意到,在LoginAction中,并没有创建任何的User类的实例对象, 这样不会出玩NullPointerException.在Struts2中不会有问题,Struts2会遍历所有请求参数,自动依次实例化任何用于填充数据的对象.例如,当Struts2尝试设置User对象name属性时,将依次用户以下方法
action.getUser();
action.setUser(new User());
action.getUser.setName(...);
Struts2尝试获得User对象的引用,当它发现User对象为null时,Struts2会调用User的默认构造方法,实例化一个User对象.



success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
	欢迎您 :   <s:property value="user.name"/>
</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>
	用户名或密码错误,请重新登陆
	<br/>
	用户名为:mgc,密码为:mgc
	<br/>
	<a href="login.jsp">登录</a>
</body>
</html>








user.java
package com.entity;

public class User {
	public User() {
		System.out.println(" in User");
	}

	private String name;
	private String password;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}







LoginAction.java
package com.action;

import com.entity.User;
import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {

	public LoginAction() {
		System.out.println("in LoginAction");
	}

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("in execute");
		if ("mgc".equalsIgnoreCase(user.getName()) && "mgc".equals(user.getPassword()))
			return SUCCESS;
		else
			return ERROR;
	}
}











web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>struts2ch03</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>login.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>
	<package name="mgc" extends="struts-default"><!-- 继承自struts-default.xml -->
		<action name="loginAction" class="com.action.LoginAction">
			<result>/success.jsp</result><!-- 如果result的name属性是success则可以省略不写 -->
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>	


到此这个小demo就完成了,代码也都贴上了,没有什么难度的...
完成
 



控制台输出
in LoginAction
in User
in execute





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值