计应134 郑寿奎
1.新建一个Web project项目
2.创建struts 2的框架(右击项目名->MyEclipse->add struts cap......)
选择 Struts 2.1
Finish完成
3.查看web.xml文件
如果找不到jsp,可能是web.xml里的 <url-pattern>*.action</url-pattern>找不到。则可以添加一个 <url-pattern>/*</url-pattern>
4.配置struts.xml文件
class里连接的*.java处理 ,result是处理返回值进行判断在连接jsp。注:可以在package里创建多个action
5.新建Action业务控制器denglu(在src里先创建一个包,在创建class,这里创建的class与struts.xml里action的那个class是呼应的)
package com; import java.sql.ResultSet; import java.sql.SQLException; import DBHelper.DBHelper; public class denglu { private String wpl; private String ad; private String a; private String d; private String c; private String r; public String getR() { return r; } public void setR(String r) { this.r = r; } public String getA() { return a; } public void setA(String a) { this.a = a; } public String getD() { return d; } public void setD(String d) { this.d = d; } public String getC() { return c; } public void setC(String c) { this.c = c; } public String getWpl() { return wpl; } public void setWpl(String wpl) { this.wpl = wpl; } public String getAd() { return ad; } public void setAd(String ad) { this.ad = ad; } public String execute() throws SQLException{ String sqlStr="select * from a"; ResultSet rs=DBHelper.getResultSet(sqlStr); if(rs.next()){ System.out.print(rs.getString("a")); System.out.print(rs.getString("b")); return "ok"; } else return "no"; } }
这里private定义的属性名与提交的*.jsp的name是一一对应的
注:用private定义了之后可以自动生成get和set(右击代码处->Source->Generate Getters and Setters...)
需要自动生成get和set的打勾
6.新建视图网页denglu.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 7 <title>登录</title> 8 9 10 11 </head> 12 13 <body> 14 15 <form name="b" id="b" method="post" action="denglu.action" style="300px;height: 100px;"> 16 17 <s:textfield name="wpl" label="用户"/> 18 <br> 19 <s:password name="ad" label="密码"/> 20 <br> 21 <s:select name="a" label="职业" list="{'学生','老师','管理员'}"/> 22 <br> 23 权限<label for="xs"> <s:checkbox name="d" id="xs" label="学生" fieldValue="学生"></s:checkbox></label> 24 <label for="ls"> <s:checkbox name="d" id="ls" label="老师" fieldValue="老师"></s:checkbox></label> 25 <label for="gly"> <s:checkbox name="d" id="gly" label="管理员" fieldValue="管理员"></s:checkbox></label> 26 <br> 27 <s:radio name="c" label="爱好" list="{'看电影','运动','玩游戏'}" /> 28 29 <br> 30 <s:textarea name="r" label="备注" cols="30" rows="5"/> 31 <s:submit value="提交"/> 32 <s:reset value="重置"/> 33 34 </form> 35 36 </body> 37 </html>
form里的action="denglu.action"与struts.xml里的action的name是对应的
7.新建成功网页ok.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>登录成功</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 登录成功 <br> 28 <s:property value="wpl"/><br> 29 <s:property value="ad"/><br> 30 <s:property value="a"/><br> 31 <s:property value="d"/><br> 32 <s:property value="c"/><br> 33 <s:property value="r"/> 34 </body> 35 </html>
8.新建错误网页no.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>登录失败</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 26 <body> 27 111 28 </body> 29 </html>
9.创建好的结构图
10.运行denglu.jsp
(1).在工具栏上单击按钮,将项目发布到Tomcat
(2).输入地址:http://localhost:8085/s1/denglu.jsp
主要思想:
前端用form标签提交到struts 2,在由struts 2提交到后台
<package name="a" extends="struts-default">
<action name="bj" class="co.bj">
<result name="ok">/bj.jsp</result>
</action>
</package>
name里的名字和JSP里要提交的一致:action="bj.action"
class里是表示转到后台的哪里处理,co是包名,bj是类名
当后台运行完的时候会返回一个值,在根据返回的这个值和<result name="ok">/bj.jsp</result>name里的值进行判断,在决定返回给哪个前台JSP。
这里放回值是ok ,转到bj.jsp页面
注意:如果不是返回给JSP页面,而是返回给其它action处理的话,那么这里就是<result name="yes" type="redirectAction">jr.action</result>
处理的时候:
格式:
package co;
public class aa {
public String execute(){
return null;
}
}
在execute()里运行在返回值