在Web中配置struts2的步骤:
- 在WEB-INF目录下的lib目录里加入必要的jar包
- 在src目录下建立struts.xml文件
- 配置web.xml的过滤器
struts.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="struts" extends="struts-default"> 8 9 10 </package> 11 </struts>
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" 3 xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 7 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 </filter> 12 13 <filter-mapping> 14 <filter-name>struts2</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping> 17 18 <welcome-file-list> 19 <welcome-file>index.jsp</welcome-file> 20 </welcome-file-list> 21 </web-app>
Struts中对信息进行验证:
关键步骤:
- 重写自定义Action的validate()方法.
- 在struts中配置错误转向页面
LoginAction.java:
1 package com.sunlfower.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class LoginAction extends ActionSupport { 6 7 private String username; 8 private String password; 9 private String repassword; 10 11 public String getUsername() { 12 return username; 13 } 14 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 19 public String getPassword() { 20 return password; 21 } 22 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 27 public String getRepassword() { 28 return repassword; 29 } 30 31 public void setRepassword(String repassword) { 32 this.repassword = repassword; 33 } 34 35 public String execute() throws Exception { 36 return SUCCESS; 37 } 38 39 /** 40 * 对信息进行校验的方法 41 */ 42 public void validate() { 43 if (null == username || "".equals(username)) 44 addActionError("用户名不能为空"); 45 if (null == password || "".equals(password) || null == repassword || "".equals(repassword)) 46 addActionError("密码不能为空"); 47 else if (!password.equals(repassword)) 48 addActionError("两次密码输入不匹配"); 49 } 50 }
struts.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="struts" extends="struts-default"> 8 <action name="loginvalidate" class="com.sunlfower.action.LoginAction"> 9 <result name="success">/welcome.jsp</result> 10 <!-- 如果输入信息的校验出错,则转回index.jsp --> 11 <result name="input">/index.jsp</result> 12 </action> 13 14 </package> 15 </struts>
index.jsp:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form action="loginvalidate.action" method="post"> 25 <table align="center" border="0"> 26 <tr> 27 <td align="right">用户名:</td> 28 <td><input type="text" size="20" name="username"/></td> 29 </tr> 30 <tr> 31 <td align="right">密码:</td> 32 <td><input type="password" size="20" name="password"/></td> 33 </tr> 34 <tr> 35 <td align="right">重新输入密码:</td> 36 <td><input type="password" size="20" name="repassword"/></td> 37 </tr> 38 <tr> 39 <td align="center" colspan="2"><input type="submit" value="提交"/></td> 40 </tr> 41 </table> 42 </form> 43 </body> 44 </html>
welcome.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@ taglib prefix="s" uri="/struts-tags"%> 5 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>欢迎光临</title> 11 </head> 12 <body> 13 <center> 14 <h3><font color="blue"><s:property value="username"/></font>,欢迎光临</h3><br> 15 </center> 16 </body> 17 </html>