struts2实现登录功能Action
struts2包可以在struts2官方网址下载,struts2采用热部署的方式注册插件,即如果向struts2中添加插件,直接把jar文件放在lib中即可,而不需要配置任何文件。
新建Web项目struts2,将struts2所以来的jar添加到/WEB-INF/lib文件夹,注意删除没有用的插件(形如xxx-plugin-2.0.11.jar的)。先用struts2实现登录功能,新建Action类LoginAction。struts2的Action继承com.opensymphony.xwork2.ActionSupport类。源代码如下:
LoginAction
import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{/** * */private static final long serialVersionUID = 1L;private String account;//账号private String password;//密码@Overridepublic String execute() throws Exception {if("helloween".equalsIgnoreCase(account) && "1234".equals(password)){return SUCCESS;}return LOGIN;}public String getAccount() {//account属性的getter方法return account;}public void setAccount(String account) {//account属性的setter方法this.account = account;}public String getPassword() {//password属性的getter方法return password;}public void setPassword(String password) {//password的setter方法this.password = password;}}
<?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="main" extends="struts-default"><global-results><!-- 所有的全局变量result --><result name="login">/login.jsp</result></global-results><action name="loginPerson" name="code"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="struts"%><!-- 标签库taglib声明 --><!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>登录成功</title></head><body>登录成功,欢迎您,<struts:property value="account"/><!-- 显示Action里的account属性 --></body></html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="struts"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>登录</title> </head> <body> <struts:form action="loginPerson"> <!-- FORM表单 --> <struts:label value="登录系统"></struts:label> <struts:textfield name="account" label="账号"></struts:textfield> <struts:password name="password" label="密码"></struts:password> <struts:submit value="登录"></struts:submit> </struts:form> </body></html>
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts的filter,所有的请求都被映射到Struts2上 --> <filter> <filter-name>struts2</filter-name><!-- filter的名称 --> <filter-class><!-- filter的入口 --> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param><!-- 该参数可省略,默认为"*.action" --> <param-name>struts2.action.extension</param-name> <param-value>action</param-value><!-- 默认为"*.action --> </init-param> </filter> <!-- Strut2的Filter的URL --> <filter-mapping> <filter-name>struts2</filter-name><!-- Filter名称 --> <url-pattern>/*</url-pattern><!-- 截获所有URL --> </filter-mapping> </web-app>