TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Sturts2漏洞百出,可谓是年年都会爆发一次或大或小的风波,但是即使这样,Sturts2还是有一批用户的。
2 p$ h0 d: d! y) P& S1 y% E花费十几分钟,简单的搭建实现了一下基本的登录功能。都是入门级的,高手肯定是不屑了,但是对于初次涉猎的小白们,还是可以提供点参考的。- Y2 S6 e+ P, X ?* ~' h. v
L0 B, H4 [! j3 E+ Y注意:
* R' q0 R8 {; x2 s( x+ T* j0)项目Demo可以正常运行并经过测试
$ G/ K6 y3 m( |1 d/ H; [ o1)代码是伪代码,自行添加逻辑 w3 u8 x" H+ @5 v$ D$ G
2)struts2-2.5.10 加入了Action验证,注意 struts.xml 中的 <global-allowed-methods>regex:.*</global-allowed-methods>
( U8 f* [8 v: c8 Z7 x: W+ s; A; w5 a U3)struts2-2.5.10 拦截器修改了路径,注意web.xml中的配置! ]# T# V7 ?& I1 j
4)本案例,返回采用json数据的形式,使用struts2-json-plugin-2.3.20.jar,json配置见struts.xml 全局配置。
$ @2 h( Q& ~, P- t% j4 U6 s8 `
: x# U/ i% J ~$ p一、项目搭建(使用最新版本struts2-core-2.5.10)) C4 ]& o, g$ W+ c' o7 A" X& ~
' h* G& k& p0 Q v. Q& c* N* S
1、相关JAR
6 c) i% Y* Z% E& a3 ncommons-fileupload-1.3.2.jar
7 N( c& c! d& _1 Z5 Q& vcommons-io-2.4.jar. S$ |4 Y' h! O
commons-lang3-3.4.jar* N5 o7 H$ K: k/ M
commons-logging-1.2.jar5 i E9 s1 A& ~# c
freemarker-2.3.23.jar( L& D3 [! l6 V6 z, `0 n; ^# p3 ^2 E2 t
javassist-3.20.0-GA.jar
( f/ I6 j& D2 o/ N8 z2 {log4j-api-2.7.jar+ N+ y) A3 G4 @ k
log4j-core-2.7.jar6 s& S& |2 I% o
ognl-3.1.12.jar
! e/ o8 _3 e7 F6 F5 f2 astruts2-core-2.5.10.jar7 n4 q4 d# l% F
struts2-json-plugin-2.3.20.jar% s! H$ z9 K9 q1 m
xwork-core-2.3.31.jar
9 H( s& c% a8 A: n2 G* L+ h; i# d8 j( o
2、前端框架
* z$ S% }3 l! A! hJqueryjquery-1.10.2.min.js
# R$ O" \4 J; \: ebootstrap.min.js# k/ V* U1 f9 l9 V
/ B& P2 ]( E+ ?) A3 v3、相关配置: }9 U) n% [, ~) n6 u& }7 H- f/ ~- s
web.xml$ B- b/ s5 A5 b; T- Q. ^" Q: W
- <?xml version="1.0" encoding="UTF-8"?>% b: x" b$ U8 x+ j
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" >
/ d1 {2 x7 { U: D4 B( ]+ C3 B - <display-name>struts2</display-name>
; [ |/ t* Z; H; J: X. J( C" y - <!-- struts2配置 -->
; W N9 ], f6 j* F/ ?- K* z' {4 x- R - <filter>
2 A7 D* h: P. g0 M6 z - <filter-name>struts2</filter-name>7 _! Z5 |3 q* c* L+ w7 T: c, @
- <filter-class>' Q( Z- B( A8 e9 ?
- org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter+ T% `3 h9 V, o. X
- </filter-class>
' x9 y4 ?; ^6 F0 k; b/ s - </filter>
9 B' H) `1 }0 q+ Z2 o! [ - <filter-mapping>* k% Z+ v' S; Y) u
- <filter-name>struts2</filter-name> ~" @" X4 g1 ]
- <url-pattern>/*</url-pattern>
7 h- m6 C y0 c+ ~ - </filter-mapping>
7 m! K* t9 z8 ?& r% A - <welcome-file-list>
9 r0 |) S: P# p, \5 _ - <welcome-file>login.jsp</welcome-file>% y% Q; G. Q- h7 Y- A
- </welcome-file-list>
A4 W+ ^9 J2 t' t- Q; A1 ^( G - </web-a
复制代码 . f* F3 h% s% Y5 v, u. p
struts.xml
% _4 m0 |& J" j R6 v6 e a# F- <?xml version="1.0" encoding="UTF-8" ?>
7 d. v q: |( l: `3 \8 ]( `4 K L - <!DOCTYPE struts PUBLIC
! x7 i# N! ^1 N4 |# n - "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4 g# Y5 g/ B2 w6 X" M1 G - "http://struts.apache.org/dtds/struts-2.5.dtd">
" m' H0 Q6 J" E$ V4 D+ { - <struts>( j& X2 g2 J. {6 q
- <!-- struts配置文件改动后,是否重新加载 -->
1 j1 `0 w" V& O+ p0 q - <constant name="struts.configuration.xml.reload" value="true" />
% E& L# R7 C5 D, n - <!-- 开发模式 开启它则默认开启了i18n.reload、configuration.xml.reload 上线后要关闭此选项 -->
x. ?" i/ C- ?7 `, e" E - <constant name="struts.devMode" value="false" />$ n, U8 F% R7 x# T
- <constant name="struts.i18n.encoding" value="UTF-8"/>
0 E( x* Y+ u" y% G h1 w - <constant name="struts.multipart.maxSize" value="1073741824"/><!-- 上传文件最大值 -->
* C% a" [0 E8 q1 Y" { - <constant name="struts.custom.i18n.resources" value="config" />! ~, f3 A) ?: N& P
- <constant name="struts.action.extension" value="action"/>" s1 I' G6 x- p, y/ l0 d* ^
- <package name="struts-global" extends="json-default">
7 A7 y X/ q, q; H - <!-- 配置Json输出 配合使用 struts2-json-plugin-2.3.20.jar 后台配置 message -->
* v5 _. x! L) I- p3 {: H. G - <global-results>
: C! O; H; c6 {% | - <result type="json">
* A2 @1 W7 q+ z8 ~' d - <param name="root">message</param>; m5 \, M; p6 H0 _
- </result>
& J( Q4 i$ }% N9 _+ ` - </global-results>) H) _* @7 O+ e' n( }
- <!-- 2.5.10版本 匹配 -->2 C5 w9 H; A- E4 W; F
- <global-allowed-methods>regex:.*</global-allowed-methods>3 c1 H9 T5 X- z( [& G' E5 X
- </package>7 Q* |* d* d V
- <package name="user" extends="struts-global">
! }% G4 q- ]3 m - <action name="userAction_*" class="com.itstyle.web.user.UserAction" method="{1}">
, _6 P: V- @/ T+ X6 `0 b+ _. x - <result name="logout">/login.jsp</result>
1 E; Y# M% H' y1 A1 }/ G - </action>0 _: d% X/ x1 x, r
- </package>' s4 m3 l; J4 _( B7 }, D5 G1 ~) _
- </struts>+ l) r; [$ T. n0 `* c0 L4 Z
6 p% B5 j0 ^& O s9 `' X
复制代码 ! B K+ Q$ U5 f2 I% m) f
0 D% c# Q2 P' u9 F, G4 C9 kUserAction) ?$ p% l. Y- J. S
- package com.itstyle.web.user;
" F r. M6 Y* l6 A8 t
4 P% Z4 ?) K0 U' D) P) Q- import com.opensymphony.xwork2.ActionSupport;
: y; L) P3 Q2 z$ P - /** r! Y) Q; G! A$ P$ l+ _1 s
- * 1 B) G9 c' a* n$ f P( m2 X, O
- * @author 科帮网! i, L7 e" I% z. p% Z
- *8 e T- m% J8 b R+ g
- */0 ?7 f8 s1 E; W1 A% ?% R
- public class UserAction extends ActionSupport {
0 I W8 b8 [1 H* Q& o - private static final long serialVersionUID = 1L;
# q3 v& s" f2 d. P6 q1 s - private String message;
& n z" ]1 R- { - private String userName;
8 P& Y5 r: Q' l6 q+ U9 \2 t4 h - private String password;
& ?) U: E0 I4 P, R5 [4 k -
# I1 C7 k! l' n& M - public String login(){, P% v. d+ N; s3 J8 f1 A
- if("admin".equals(userName)&&"admin".equals(password)){
/ v9 T* Q2 I& e* A: T8 O8 N9 `5 a - message = "0";
! u* N; {# z9 I! p+ r - }else{9 C8 A: c( e+ l* ~: e
- message = "1";
3 [+ V- \7 Z6 H! Y - }2 `& ?3 X+ O: P7 b
- return SUCCESS;
& Q) t4 [0 Y6 J - }/ `; _2 T2 b S7 [8 u; A: k
- 4 z" y* V; Y8 i3 x6 r) _5 Y
- public void setMessage(String message) {
$ l: ?. {$ |5 l6 o" I/ m - this.message = message;7 q& F2 L( G2 p0 ]9 L0 f
- }+ ~4 {! P3 ~- o
- public String getMessage() {! ^5 [8 u" t N) k
- return message;- ?! H, f. o p5 `5 Z6 V
- }1 U4 e, D a4 l& Q/ U7 I
6 Y. R$ r9 F! J2 n- public void setUserName(String userName) {0 n* i% S' h5 a+ w" @' X
- this.userName = userName;
5 R( d0 V# l* l0 i/ o - }
: t& o1 S4 B: G5 c! E0 v5 N4 l4 _ - / M1 h4 R! O3 ?; X
- public void setPassword(String password) {
$ k9 Q& q& j& m9 ~ - this.password = password;* }% H5 e1 b9 R0 s7 V
- } C7 H$ y; ?) q% q9 C* U' P- _
- }; } s. p/ f0 f2 |; r7 ~
复制代码
6 ]3 @. C% c' I/ z: {. S; g: I& _% X) t8 \% y
login.jsp! `/ N- e0 W8 L9 c6 b. K. [* y
- <script>
7 f( A: V, u8 o7 a/ p& J - var path = '<%=basePath %>';
2 A% ^3 y) l) E2 C - function login(){
5 Z8 z2 o1 `/ m& M1 ]4 ~2 D& Z* u8 t - var username = $("#username").val();0 a" F5 X& T' S' W, d/ ^# u% o0 L
- var password = $("#password").val();
; \7 x; }( x2 `" N- ^ - var data = {username:username,password:password};8 @* c" G8 n) i C9 j
- $.ajax({
6 T7 _7 e: T$ P& u% K. s9 V - type:"post",
+ {) A, i" y# i# w - url:path+"userAction_login.action",
& ]% P& e7 z3 q/ n/ W5 M9 G5 P - data:data,7 v* O; ?$ O% F( l
- dataType:"json",- S1 X d6 G2 |: k, h! @
- async : false,9 U& Q; C0 m/ p0 h
- success : function(data) {
5 Z& L5 w) R3 K* \" S - if(data==="0"){: |1 T$ s" _' e: T5 r4 L
- alert("登录成功");
# b4 T( R; L- `# n0 u - window.location.href ="http://blog.52itstyle.com";
0 f7 J6 d' l; ~* V* X - }else{
. ~& H# a+ ?9 h - alert("登录失败");
- L" Y+ j* x/ E& W B* t/ } - }; Y# \ p3 r5 |" V0 i# R
- }8 k; c4 V; J8 m
- });
5 y `. l1 Q) s/ x3 ] -
) m6 v2 ^' v+ Y* [: x- A - }
0 ^$ M! ]1 c+ K9 d! ?1 e8 f
. D4 ?4 u9 ~9 M2 l0 g- </script>
复制代码
; g. y( s. z0 p7 }3 F页面展示:
9 w1 ^& Y2 t" M$ x; N* \
1 K0 G3 I: a" G# e! ?6 ^% d3 }8 S+ X) _
8 X$ h6 E6 {, @0 Y9 b2 y- x3 w5 p& m
$ }# S! t4 H. M5 v
基于Sturts2实现一个简单的登录功能.txt
(64 Bytes, 下载次数: 2, 售价: 1 IT币)
4 r' i1 v! c& D' c& ^2 B
|
|