Struts2的一个入门实例----登录功能
Posted Asu_PC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2的一个入门实例----登录功能相关的知识,希望对你有一定的参考价值。
一、搭建环境与测试
1、web.xml文件,配置核心Filter
1: <?xml version="1.0" encoding="UTF-8"?>
2: <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3: xmlns="http://java.sun.com/xml/ns/j2ee"
4: xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
5: xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
6: id="WebApp_9" version="2.4">
7: <filter>
8: <filter-name>struts2</filter-name>
9: <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
10: </filter>
11: <filter-mapping>
12: <filter-name>struts2</filter-name>
13: <url-pattern>/*</url-pattern>
14: </filter-mapping>
15: <filter>
16: <filter-name>struts-cleanup</filter-name>
17: <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
18: </filter>
19: <filter-mapping>
20: <filter-name>struts-cleanup</filter-name>
21: <url-pattern>/*</url-pattern>
22: </filter-mapping>
23: </web-app>
2、将框架的类库放在WEB-INF/lib路径下
3、用户请求页面
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2: pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags" %>
4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5: <html>
6: <head>
7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8: <title>login</title>
9: </head>
10: <body>
11: <s:form action="login" align="center">
12: <s:textfield name="username" key="user" value="用户名"/>
13: <s:textfield name="password" key="pass" value="密 码"/>
14: <s:submit value="登录" key="login"/>
15: </s:form>
16: </body>
17: </html>
4、实现控制器类
Action就是一个包含execute方法的普通java类。代码如下
LoginAction.java
1: package cc.openhome;
2:
3: import com.opensymphony.xwork2.Action;
4: import com.opensymphony.xwork2.ActionContext;
6:
7: public class LoginAction implements Action {
8: private String username;
9: private String password;
10: public String getUsername() {
11: return username;
12: }
13: public void setUsername(String username) {
14: this.username = username;
15: }
16: public String getPassword() {
17: return password;
18: }
19: public void setPassword(String password) {
20: this.password = password;
21: }
22:
23: public String execute() throws Exception {
24: // TODO Auto-generated method stub
25: ActionContext ctx=ActionContext.getContext();
26: if(getUsername().equals("nihao") && getPassword().equals("nihao")){
27: ctx.getSession().put("user", getUsername());
28: return SUCCESS;
29: }else{
30: return ERROR;
31: }
32: }
33: }
34:
5、配置Action
修改struts.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="struts2" extends="struts-default">
8: <action name="login" class="cc.openhome.LoginAction">
9: <result name="success">/success.jsp</result>
10: <result name="error">/error.jsp</result>
11: </action>
12: </struts>
6、success.jsp页面
1: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2: <%@ taglib prefix="s" uri="/struts-tags" %>
3: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4: <html>
5: <head>
6: <title>success</title>
7: </head>
8: <body>
9: <%=request.getAttribute("username")%>:您已经登录!
10: </body>
11: </html>
12:
7、error.jsp页面
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2: pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags"%>
4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5: <html>
6: <head>
7: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8: <title>error</title>
9: </head>
10: <body>
11: <%=request.getAttribute("username")%>:登录失败,请重新登录!
12: </body>
13: </html>
8、运行结果
二、运行机制处理流程
三、定义国际化资源
1、定义两个语言资源文件,并且命名为message_zh_CN.properties和message_en_US.properties
message_zh_CN.properties
1: successPage=\\u767B\\u5F55\\u6210\\u529F\\u9875\\u9762
2: failPage=\\u767B\\u5F55\\u5931\\u8D25\\u9875\\u9762
3: user=\\u7528\\u6237\\u540D
4: pass=\\u5BC6\\u7801
5: login=\\u767B\\u5F55
6: welcome=\\u6B22\\u8FCE\\uFF0C
7: fail=\\u767B\\u5F55\\u5931\\u8D25\\uFF0C
message_en_US.properties
1: successPage=successPage
2: failPage=failPage
3: user=username
4: pass=password
5: login=login
6: welcome=welcome,
7: fail=fail,
2、修改success.jsp
输出国际化信息的方式有两种:
(1)、<s:text name=”welcome” />
(2)、<s:property value=”%{getText(“welcome”)}” />
1: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
2: <%@ taglib prefix="s" uri="/struts-tags" %>
3: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4: <html>
5: <head>
6: <title><s:text name="successPage"/></title>
7: </head>
8: <body>
9: <%=request.getAttribute("username")%>:<s:text name="welcome"/>
10: 您已经登录!
11: </body>
12: </html>
3、修改error.jsp
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2: pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags"%>
4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5: <html>
6: <head>
7: <title><s:text name="successPage"/></title>
8: </head>
9: <body>
10: <%=request.getAttribute("username")%>:<s:text name="fail"/>请重新登录!
11: </body>
12: </html>
4、在struts.xml中添加<constant name="struts.custom.i18n.resources" value="message"/>
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: <constant name="struts.custom.i18n.resources" value="message"/>
8: <action name="login" class="cc.openhome.LoginAction">
9: <result name="success">/success.jsp</result>
10: <result name="error">/error.jsp</result>
12: </action>
13: </struts>
5、运行结果
三、增加数据校验
继承ActionSupport类,它是一个工具类,实现了Action接口,还实现了Validateable接口,提供了数据校验功能。在Validateable接口中,定义了一个validate()方法,如果校验表单输入域出现错误,则将错误添加到ActionSupport类的fieldErrors域中,通过OGNL表达式负责输出。则LoginAction修改后为:
1、LoginAction.java
1: package cc.openhome;
2:
3: import com.opensymphony.xwork2.Action;
4: import com.opensymphony.xwork2.ActionContext;
5: import com.opensymphony.xwork2.ActionSupport;
6:
7: public class LoginAction extends ActionSupport implements Action {
8: private String username;
9: private String password;
10: public String getUsername() {
11: return username;
12: }
13: public void setUsername(String username) {
14: this.username = username;
15: }
16: public String getPassword() {
17: return password;
18: }
19: public void setPassword(String password) {
20: this.password = password;
21: }
22:
23: public String execute() throws Exception {
24: // TODO Auto-generated method stub
25: ActionContext ctx=ActionContext.getContext();
26:
27: if(getUsername().equals("nihao") && getPassword().equals("nihao")){
28: ctx.getSession().put("user", getUsername());
29: return SUCCESS;
30: }else{
31: return ERROR;
32: }
33: }
34: public void validate(){
35: if(getUsername()!=null || getUsername().trim().equals("")){
36: addFieldError("username", "user.required");
37: }
38: if(getPassword()!=null || getPassword().trim().equals("")){
39: addFieldError("password", "pass.required");
40: }
41: }
42: }
43:
2、修改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: <constant name="struts.custom.i18n.resources" value="message"/>
8: <action name="login" class="cc.openhome.LoginAction">
9: <result name="success">/success.jsp</result>
10: <result name="error">/error.jsp</result>
11: <result name="input">/denglu.jsp</result>
12: </action>
13: </struts>
3、运行结果
(1)、message_zh_CN.properties
1: successPage=\\u767B\\u5F55\\u6210\\u529F\\u9875\\u9762
2: failPage=\\u767B\\u5F55\\u5931\\u8D25\\u9875\\u9762
3: user.required=\\u7528\\u6237\\u540D\\u5FC5\\u987B\\u6B63\\u786E\\u8F93\\u5165
4: pass.required=\\u5BC6\\u7801\\u5FC5\\u987B\\u6B63\\u786E\\u8F93\\u5165
5: user=\\u7528\\u6237\\u540D
6: pass=\\u5BC6\\u7801
7: login=\\u767B\\u5F55
8: welcome=\\u6B22\\u8FCE\\uFF0C
9: fail=\\u767B\\u5F55\\u5931\\u8D25\\uFF0C
(2)、message_en_US.properties
1: successPage=successPage
2: user.required=user.required
3: pass.required=pass.required
4: failPage=failPage
5: user=username
6: pass=password
7: login=login
8: welcome=welcome,
9: fail=fail,
(3)、修改validate()方法
1: public void validate(){
2: if(getUsername()!=null || getUsername().trim().equals("")){
3: addFieldError("username", getText("user.required"));
4: }
5: if(getPassword()!=null || getPassword().trim().equals("")){
6: addFieldError("password", getText("pass.required"));
7: }
8: }
(4)、运行结果
5、使用Struts2的校验框架
如果不需要写大量代码,可以用基于XWork的validator框架,新建一个校验规则文件即可。把文件命名为: LoginAction-validation.xml
LoginAction-validation.xml
1: <?xml version="1.0" encoding="UTF-8"?>
2: <!DOCTYPE validators PUBLIC
3: "-//Apache Struts//XWork Validator 1.0//EN"
4: "http://struts.apache.org/dtds/xwork-validator-1.0.dtd">
5: <validators>
6: <field name="username">
7: <field-validator type="requiredstring">
8: <message key="user.required"/>
9: </field-validator>
10: </field>
11: <field name="password">
12: <field-validator type="requiredstring">
13: <message key="pass.required"/>
14: </field-validator>
15: </field>
16: </validators>
以上是关于Struts2的一个入门实例----登录功能的主要内容,如果未能解决你的问题,请参考以下文章