代码改变世界

struts配置及检验

2018-05-29 19:01  HHFFZ  阅读(158)  评论(0编辑  收藏  举报
登录页面
<%@ 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>Insert title here</title> </head> <body> <center> <s:fielderror/> <form action="hello" method="post"> 账号:<input type="text" name="username"/><br /> 密码:<input type="password" name="password"/><br /> <input type="submit" value="登录"/> </form> </center> </body> </html>


struts配置及web配置
<?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="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.action.HelloAction">
            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>
            
        </action>
    </package>
</struts>





<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
  <display-name>Struts Blank</display-name>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


参数检验类HelloActio
package com.action;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
	
private static final long serialVersionUID=1L;
private String username;
private String pasdword;
public String getUsername() {
	return username;
}


public void setUsername(String username) {
	this.username = username;
}


public String getPasdword() {
	return pasdword;
}


public void setPasdword(String pasdword) {
	this.pasdword = pasdword;
}

 public void validateUsername()
 {
	 
	 if(StringUtils.isBlank(username)) {
		 addFieldError(username, "名字不能空");
	 }
 }


@Override
	public String execute() throws Exception {
		
		return "success";
	}
}

 

校验结束,数据正确页面;
<%@ 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>Insert title here</title>
</head>
<body>

   执行
 
</body>
</html>