Struts2的入门示例

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: 小 | 中 | 大 | 超大  

一个简单的Struts2的使用示例,主要功能是检查用户从jsp页面中输入的用户名是否与密码一致。

首先是jsp部分的代码。jsp页面做了一个form表单的请求,和Ajax的请求。

1<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>2<%3Stringpath=request.getContextPath();4StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5%>67<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">8<html>9<head>10<basehref="<%=basePath%>">1112<title>Hello Struts2</title>13<metahttp-equiv="PRagma"content="no-cache">14<metahttp-equiv="cache-control"content="no-cache">15<metahttp-equiv="expires"content="0">16<metahttp-equiv="keyWords"content="keyword1,keyword2,keyword3">17<metahttp-equiv="description"content="This is my page">18<!--19<link rel="stylesheet" type="text/CSS" href="styles.css">20-->21<scriptsrc="./dist/jquery-2.0.0.min.js"></script>22<styletype="text/css">23input{24width:100%25}26</style>27</head>2829<body>30<h1>Hello Struts2</h1>31<div>32<h2>Form表单</h2>33<formaction="UserLoginAction.action">34<table>35<tr>36<td>用户名:</td><td><inputtype="text"name="username"></td>37</tr>38<tr>39<td>密码:</td><td><inputtype="password"name="password"></td>40</tr>41<tr><td><inputtype="submit"value="提交"></td></tr>42</table>43</form>44</div>45<div>46<h2>AJAX调用</h2>47<table>48<tr><td>用户名:</td><td><inputid="usernames"></td></tr>49<tr><td>密码:<td><inputid="passwords"type="password"></td></tr>50<tr><td><buttononclick="check()">提交</button></td></tr>51</table>5253</div>5455</body>5657<scripttype="text/Javascript">58functioncheck(){59$.post("/UserLoginStruts2/UserLoginAction2.action",{60password:document.getElementById("passwords").value,61username:document.getElementById("usernames").value,6263},function(data,status){64alert(data);65});66}67</script>6869</html>

View Code

form表单请求action后,根据结果进行跳转,两个跳转页面的代码。

1<%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>2<%3Stringpath=request.getContextPath();4StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5%>67<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">8<html>9<head>10<basehref="<%=basePath%>">1112<title>My JSP 'fail.jsp' starting page</title>1314<metahttp-equiv="pragma"content="no-cache">15<metahttp-equiv="cache-control"content="no-cache">16<metahttp-equiv="expires"content="0">17<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">18<metahttp-equiv="description"content="This is my page">19<!--20<link rel="stylesheet" type="text/css" href="styles.css">21-->2223</head>2425<body>26Fail<

br>27</body>28</html>

View Code

1<%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>2<%3Stringpath=request.getContextPath();4StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";5%>67<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">8<html>9<head>10<basehref="<%=basePath%>">1112<title>My JSP 'result.jsp' starting page</title>1314<metahttp-equiv="pragma"content="no-cache">15<metahttp-equiv="cache-control"content="no-cache">16<metahttp-equiv="expires"content="0">17<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">18<metahttp-equiv="description"content="This is my page">19<!--20<link rel="stylesheet" type="text/css" href="styles.css">21-->2223</head>2425<body>26TRUE27</body>28</html>

View Code

form表单的请求会根据结果进行跳转;利用AJAX请求,会将判断结果,返回给jsp页面,并提示。

然后是两个Action部分的代码。

form表单所请求的action代码

1packagestruts;23importcom.opensymphony.xwork2.ActionSupport;45publicclassUserLoginActionextendsActionSupport{6privateString username,password;78@Override9publicString execute()throwsException {10System.out.println("Checking...");11if(this.username.equals(this.password)){12return"Success";13}else{14return"Fail";15}16}1718publicString getUsername() {19returnusername;20}2122publicvoidsetUsername(String username) {23this.username =username;24}2526publicString getPassword() {27returnpassword;28}2930publicvoidsetPassword(String password) {31this.password =password;32}33343536}

View Code

AJAX所请求的Action代码

1packagestruts;23importorg.apache.struts2.ServletActionContext;45importcom.opensymphony.xwork2.ActionSupport;67publicclassUserLoginAction2extendsActionSupport{8privateString username,password;910@Override11publicString execute()throwsException {12System.out.println("Checking2...");13System.out.println(password);14System.out.println(username);15System.out.println(password.equals(username));16String str=this.password.equals(this.username)?"Success":"Fail";17ServletActionContext.getResponse().getWriter().print(str);18returnnull;19}2021publicString getUsername() {22returnusername;23}2425publicvoidsetUsername(String username) {26System.out.println("SET:"+username);27this.username =username;28}2930publicString getPassword() {31returnpassword;32}3334publicvoidsetPassword(String password) {35this.password =password;36}37383940}

View Code

Struts的配置文件。

1<?xmlversion="1.0" encoding="UTF-8"?>2<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">3<struts>45<packagename="struts"namespace="/"extends="struts-default">6<actionname="UserLoginAction"class="struts.UserLoginAction">7<paramname="username">default</param>8<paramname="password">defaults</param>9<resultname='Fail'>/fail.jsp</result><resultname='Success'>/true.jsp</result></action>10<actionname="UserLoginAction2"11class="struts.UserLoginAction2">12<paramname="username"></param>13<paramname="password"></param>14</action></package></struts>

View Code

WEB的配置文件。

1<?xml version="1.0" encoding="UTF-8"?>2<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1">3<display-name>UserLoginStruts2</display-name>4<filter>5<filter-name>struts2</filter-name>6<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>7</filter>8<filter-mapping>9<filter-name>struts2</filter-name>10<url-pattern>*.action</url-pattern>11</filter-mapping>12</web-app>

View Code

通过这个实例,主要是为了熟悉Struts2的基本流程,利用MyEclipse2014开发Struts2的项目还是很方便的。只需要将所需要的Action写好,同时在struts.xml中配置好Action与具体的类的关系和一些参数信息。就可以了。

作为初学者,代码中有什么漏洞或者不规范的地方,希望给位大牛多多指教!!!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有