Struts2 访问Servlet API(ActionContext)以及校验框架
2017-07-02 10:19
363 查看
访问Servlet API
Action类:package com.Actions; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { 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 fun3(){ if("admin".equals(this.username) &&"123".equals(this.password)){ ActionContext context=ActionContext.getContext(); if(null !=context.getSession().get("uName")){ String msg=this.username+": 你已经登录了"; System.out.println(msg); } else{ context.getSession().put("uName",username); } return SUCCESS; } else{ String msg="登陆错误,请检查用户名和密码"; System.out.println(msg); return "error"; } } }
Struts2.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- <action name="index" class="com.Actions.HelloWorldAction"> <result> /index2.jsp </result> </action> <action name="name" method="get" class="com.Actions.GetFormValuesAction"> <result name="get"> /index2.jsp </result> </action> --> <action name="ServletAPI" method="fun3" class="com.Actions.LoginAction"> <result name="error">/index3.jsp</result> <result >/index2.jsp</result> </action> </package> </struts>
index3.jsp:
<form action="ServletAPI.action" method="post" > <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="提交"> </form>
校验框架
Struts2两种校验方式1.Action类:
package com.Actions; import com.opensymphony.xwork2.ActionSupport; public class LoginvalidateAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String username; private String password; private String FORWORD=null; public String execute(){ return SUCCESS; } 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; } }
2.在该Action相同目录下建一个xml文件,
命名为:ActionName-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message key="username.empty"/> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message key="密码不能为空"/> </field-validator> <field-validator type="stringlength"> <param name="minlength">6</param> <param name="maxlength">12</param> <message key="username.size"></message> </field-validator> </field> </validators>
3.在Struts.xml文件中配置Action,视图一定是input
<action name="validate" class="com.Actions.LoginvalidateAction"> <result name="input">/loginvalidate.jsp</result> </action>
添加一个loginvalidate.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>FirstStruts2</title> </head> <form action="ServletAPI.action" method="post" > <s:fielderror/> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="提交"> </form> <body> </body> </html>
4.运行url
http://localhost:8080/Struts2kuangjia/validate
相关文章推荐
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- ActionContext和ServletActionContext区别以及action访问servlet API的三种方法
- 升级到struts-2.3.14.3之后涉及的改动以及ActionContextCleanUp,StrutsFilter详解
- 3.5.4: 使用ServletActionContext访问Servlet API
- ServletActionContext访问Servlet API
- Struts2(6):使用ServletActionContext访问Servlet API
- Struts的 action访问servlet API的三种方法
- 升级到struts-2.3.14.3之后涉及的改动以及ActionContextCleanUp,StrutsFilter详解
- Struts2.0 Action 访问 Servlet API
- Struts之Action(Action继承类、实现接口、访问servlet API方法)
- Struts2.x中的Action访问Servlet API方式
- 升级到struts-2.3.14.3之后涉及的改动以及ActionContextCleanUp,StrutsFilter详解
- Struts 2读书笔记-----Action访问Servlet API
- Struts 2读书笔记-----Action访问Servlet API
- 使用ServletActionContext访问Servlet API
- 升级到struts-2.3.14.3之后涉及的改动以及ActionContextCleanUp,StrutsFilter详解
- Action使用ActionContext访问Servlet API