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

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

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

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

1<?xml version="1.0" encoding="UTF-8"?> 2<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 <filter> 6 <filter-name>struts2</filter-name> 7 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> 13</web-app>

    再配置struts.xml文件,此文件一定要位于src文件目录下,否则配置无效。我做此项目时由于此文件没放在src目录下,而是放在了action所在的包中,导致一直运行不出来,浪费的半天时间。 struts.xml内容如下:

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6<struts> 7 <constant name="struts.devMode" value="true" /> 8 <constant name="struts.i18n.encoding" value="UTF-8" /> 9 <constant name="struts.i18n.reload" value="true" /> 10 <constant name="struts.configuration.xml.reload" value="true" /> 11 <constant name="struts.action.extension" value="do,action,," /> 12</struts>

再看用户的请求页面:

1<%@ page language="java" contentType="text/html; charset=GBK" 2 pageEncoding="GBK"%> 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4<%@taglib uri="/struts-tags" prefix="s"%> 5<html> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 8<title>Welcome User</title> 9<s:head /> 10</head> 11<body> 12<s:form action="welcome"> 13 <s:textfield name="userName" label="User Name" /> 14 <s:submit /> 15</s:form> 16<s:debug></s:debug> 17</body> 18</html>

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

1package org.sunhing.actions; 2 3import org.apache.struts2.convention.annotation.Action; 4import org.apache.struts2.convention.annotation.Result; 5import com.opensymphony.xwork2.ActionSupport; 6@Action(value="/welcome",results={ 7 @Result(name = "success", location = "/welcome.jsp"), 8 @Result(name = "input", location = "/index.jsp")} 9) 10public class Welcome extends ActionSupport{ 11 private static final long serialVersionUID = 1L; 12 private String userName; 13 private String message; 14 15 public String execute() { 16 setMessage("Hello " + getUserName()); 17 return SUCCESS; 18 } 19 20 @Override 21 public void validate() { 22 if("".equals(userName)){ 23 addFieldError("userName", "用户名不能为空!"); 24 } 25 } 26 27 public void setUserName(String userName) { 28 this.userName = userName; 29 } 30 31 public void setMessage(String message) { 32 this.message = message; 33 } 34 35 public String getUserName() { 36 return userName; 37 } 38 39 public String getMessage() { 40 return message; 41 } 42}

     以上代碼中的注解:

@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,如下左图。

若校验通过则跳转到welcome.jsp,如上右图。下面是welcome.jsp页面代码:

1<%@ page language="java" contentType="text/html; charset=GBK" 2 pageEncoding="GBK"%> 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4<%@taglib uri="/struts-tags" prefix="s"%> 5<html> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 8<title>Insert title here</title> 9</head> 10<body> 11<s:property value="message"></s:property> 12<s:debug></s:debug> 13</body> 14</html>

    总结一下,Struts2为我们封装了很多常用的功能,避免了我们“重复造轮子”。

  •     请求参数的自动获取。我们要做的是保证表单与要处理的action中的属性名一致即可(准确的说只需保持与属性对应getXXX()方法中的XXX一致即可)。
  •      页面的自动跳转。 ActionSupport类已经为我们封装了大量常用的方法,继承此类后我们可以直接调用此类的很多方法,完成我们需要的功能。
  •     提供<s:debug></s:debug>标签,大大的方便了我们的调试。
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang