Struts2基于注解的登录与验证示例

    Struts2是个优秀的MVC框架。有人说java的框架是把简单的东西搞得复杂,而我不这样认为。学习框架以前可能会这样认为,学了以后就会感受框架固然是在简化问题,不然也不会有这么多人用了。本文介绍如何用eclipse如何建立一个Struts2基于注解的登录验证动态网页工程。通俗的说来,咱们用浏览器发送一个请求到服务器,服务器验证经过则进入欢迎页,不然给出错误提示。html

     在eclipse中新建名为Struts2Demo项目,要在项目中使用Struts2框架,先要下载Struts2的相关jar包,在本站能够很容易搜索到。而后把须要的jar包添加到项目的lib目录中。项目各文件结构以下图:java

    为了使项目支持Struts2框架,配置web.xml文件:web

<?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">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

    再配置struts.xml文件,此文件必定要位于src文件目录下,不然配置无效。我作此项目时因为此文件没放在src目录下,而是放在了action所在的包中,致使一直运行不出来,浪费的半天时间。 struts.xml内容以下:apache

<?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>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.action.extension" value="do,action,," />
</struts>

再看用户的请求页面:浏览器

<%@ 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">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Welcome User</title>
<s:head />
</head>
<body>
<s:form action="welcome">
	<s:textfield name="userName" label="User Name" />
	<s:submit />
</s:form>
<s:debug></s:debug>
</body>
</html>

上面的页面中form的action为welcome意思是页面提交到名为welcome页面或action处理。产后的Url请求为http://localhost:8080/Struts2Demo/welcome。而后咱们建立一个处理此请求的action。服务器

package org.sunhing.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(value="/welcome",results={
		@Result(name = "success", location = "/welcome.jsp"),
		@Result(name = "input", location = "/index.jsp")}
)
public class Welcome extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String userName;
	private String message;
	
	public String execute() {
		setMessage("Hello " + getUserName());
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		if("".equals(userName)){
			addFieldError("userName", "用户名不能为空!");
		}
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUserName() {
		return userName;
	}

	public String getMessage() {
		return message;
	}
}

     以上代碼中的注解:
@Action(value="/welcome",results={ @Result(name = "success", location = "/welcome.jsp"), @Result(name = "input", location = "/index.jsp")} )

    意思即为当咱们的请求为/weclome(写全了为http://localhost:8080/Struts2Demo/welcome)时此请求交给此action处理。当上面的validate函数中校验有错误时会返回"input",此时Struts2找到要跳转的jsp页面index.jsp,以下左图。app

若校验经过则跳转到welcome.jsp,如上右图。下面是welcome.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">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<s:property value="message"></s:property>
<s:debug></s:debug>
</body>
</html>

    总结一下,Struts2为咱们封装了不少经常使用的功能,避免了咱们“重复造轮子”。
eclipse

  •     请求参数的自动获取。咱们要作的是保证表单与要处理的action中的属性名一致便可(准确的说只需保持与属性对应getXXX()方法中的XXX一致便可)。
  •      页面的自动跳转。 ActionSupport类已经为咱们封装了大量经常使用的方法,继承此类后咱们能够直接调用此类的不少方法,完成咱们须要的功能。
  •     提供<s:debug></s:debug>标签,大大的方便了咱们的调试。
相关文章
相关标签/搜索