struts2学习(2)struts2核心知识
一、Struts2 get/set 自动获取/设置数据
根据上一章。中的源码继续。
HelloWorldAction.java中private String name,自动获取/设置name:
package com.cy.action; import com.opensymphony.xwork2.Action; public class HelloWorldAction implements Action{ private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String execute() throws Exception {
System.out.println("执行了Action的默认方法");
return SUCCESS;
} }
helloWorld.jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name }你好!!
</body>
</html>
1)使用get方式:
浏览器中访问:http://localhost:8080/Struts2Chap01/hello?name=asdasd
结果:
2)post方式:
编写index.jsp页面:
<body>
<form action="hello" method="post">
name: <input type="text" name="name"/>
<input type="submit" value="submit" />
</form>
</body>
浏览器中访问:http://localhost:8080/Struts2Chap01/index.jsp,输入name:
点击submit:
二、ActionSupport 类引入
HelloWorldAction2.java中继承ActionSupport:
package com.cy.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction2 extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String execute() throws Exception {
System.out.println("执行了HelloWorldAction2 Action的默认方法");
return SUCCESS;
} }
struts.xml中修改为com.cy.action.HelloWorldAction2:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 可以创建很多的package,用name来区分 -->
<package name="helloWorld" extends="struts-default">
<action name="hello" class="com.cy.action.HelloWorldAction2">
<!-- 默认是转发,转发到helloWorld.jsp -->
<result name="success">helloWorld.jsp</result>
</action>
</package> </struts>
测试结果ok;
package com.cy.action; import com.cy.model.User;
import com.cy.service.UserService;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private UserService userService = new UserService();
private String userName;
private String password; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String execute() throws Exception {
System.out.println("执行了UserAction Action的默认方法");
User user = new User();
user.setUserName(userName);
user.setPassword(password);
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
} } }
com.cy.model.User:
package com.cy.model; public class User {
private String userName;
private String password; public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
com.cy.service.UserService:
package com.cy.service; import com.cy.model.User; public class UserService { public boolean login(User user){
if("cy".equals(user.getUserName()) && "123456".equals(user.getPassword())){
return true;
}else{
return false;
}
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 可以创建很多的package,用name来区分 -->
<package name="helloWorld" extends="struts-default">
<action name="hello" class="com.cy.action.HelloWorldAction2">
<!-- 默认是转发,转发到helloWorld.jsp -->
<result name="success">helloWorld.jsp</result>
</action> <action name="user" class="com.cy.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package> </struts>
login.jsp登录界面:
<body>
<form action="user" method="post">
用户名: <input type="text" name="userName"/><br>
密码: <input type="password" name="password"/><br>
<input type="submit" value="登录" />
</form>
</body>
success.jsp登录成功的页面:
<body>
登录成功!
</body>
error.jsp登录失败的界面:
<body>
登录失败!
</body>
测试:
浏览器访问http://localhost:8080/Struts2Chap01/login.jsp,输入cy,123456
登录成功:
B:javaBean类型属性驱动:
将上面的UserAction中改为private User user;javaBean的方式:
package com.cy.action; import com.cy.model.User;
import com.cy.service.UserService;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private UserService userService = new UserService();
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String execute() throws Exception {
System.out.println("执行了UserAction Action的默认方法 采用javaBean");
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
} } }
login.jsp登录界面需要修改为如下:
<body>
<form action="user" method="post">
用户名: <input type="text" name="user.userName"/><br>
密码: <input type="password" name="user.password"/><br>
<input type="submit" value="登录" />
</form>
</body>
测试结果ok
第二种:模型驱动:
UserAction.java: //主要实现ModelDriven<User>, private User user = new User(),这里要new一个user:
package com.cy.action; import com.cy.model.User;
import com.cy.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User>{ private static final long serialVersionUID = 1L;
private UserService userService = new UserService();
private User user = new User(); public String execute() throws Exception {
System.out.println("执行了UserAction Action的默认方法 采用模型驱动");
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
}
} public User getModel() {
return user;
} }
login.jsp:
<body>
<form action="user" method="post">
用户名: <input type="text" name="userName"/><br>
密码: <input type="password" name="password"/><br>
<input type="submit" value="登录" />
</form>
</body>
四、Struts2处理传入多个值
1、需求:选择多个复选框,将复选框选中的值传入到后台;
2、需求:前端批量添加多个Student到后台,后台接受
HobbyAction.java:
package com.cy.action; import com.opensymphony.xwork2.Action; public class HobbyAction implements Action{ private String[] hobby; public String[] getHobby() {
return hobby;
} public void setHobby(String[] hobby) {
this.hobby = hobby;
} public String execute() throws Exception {
System.out.println("执行了HobbyAction的默认方法");
for(String h : hobby){
System.out.println(h);
}
return SUCCESS;
} }
StudentAction.java:
package com.cy.action; import java.util.List; import com.cy.model.Student;
import com.opensymphony.xwork2.Action; public class StudentAction implements Action{ private List<Student> students; public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
} public String execute() throws Exception {
System.out.println("执行了StudentAction的默认方法");
for(Student s : students){
System.out.println(s);
}
return SUCCESS;
} }
com.cy.model.Student.java:
package com.cy.model; public class Student { private String name;
private String sex;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
} }
struts.xml配置:
<struts> <package name="helloWorld" extends="struts-default">
<action name="hobby" class="com.cy.action.HobbyAction">
<result name="success">success.jsp</result>
</action> <action name="student" class="com.cy.action.StudentAction">
<result name="success">success.jsp</result>
</action>
</package> </struts>
hobby.jsp:
<body>
<form action="hobby" method="post">
爱好:
<input type="checkbox" name="hobby" value="唱歌"/>唱歌
<input type="checkbox" name="hobby" value="跳舞"/>跳舞
<input type="checkbox" name="hobby" value="睡觉"/>睡觉
<input type="checkbox" name="hobby" value="玩CF"/>玩CF
<input type="submit" value="提交"/>
</form>
</body>
addstudents.jsp:
<body>
<form action="student" method="post">
<table>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<tr>
<!-- 这边students[0].name命名有规范的,不然action中取不到 -->
<td><input type="text" name="students[0].name"/></td>
<td><input type="text" name="students[0].sex"/></td>
<td><input type="text" name="students[0].age"/></td>
</tr>
<tr>
<td><input type="text" name="students[1].name"/></td>
<td><input type="text" name="students[1].sex"/></td>
<td><input type="text" name="students[1].age"/></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
success.jsp:
<body>
OK!
</body>
测试结果:
1)
2)
五、struts.xml的配置:
<struts> <package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.cy.action.ForeStudent">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package> <package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.cy.action.BackStudent" method="show">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package> <!-- 抽象包,让别人继承,比如过滤敏感词汇啊啥的 -->
<package name="infoFilter" abstract="true"></package> </struts>
com.cy.action.ForeStudent.java:
package com.cy.action; import com.opensymphony.xwork2.ActionSupport; public class ForeStudent extends ActionSupport {
public String execute() throws Exception {
System.out.println("执行了ForeStudent Action的默认方法");
return SUCCESS;
} }
com.cy.action.BackStudent.java:
package com.cy.action; import com.opensymphony.xwork2.ActionSupport; public class BackStudent extends ActionSupport{
public String execute() throws Exception {
System.out.println("执行了BackStudent Action的默认方法");
return SUCCESS;
} public String show() throws Exception{
System.out.println("执行了BackStudent show方法");
return SUCCESS;
}
}
测试结果:
1)/fore/studentList
2)/back/studentList:
console打印:
执行了BackStudent show方法
struts2学习(2)struts2核心知识的更多相关文章
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...
- Struts2学习一----------Struts2的工作原理及HelloWorld简单实现
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...
- struts2学习(4)struts2核心知识III
一.result配置: result - name 就是前面返回的值,success,error等. type: dispatcher. 默认是这个,底层是jsp的forward: redirect: ...
- struts2学习(3)struts2核心知识II
一.struts.xml配置: 1.分模块配置方法: 比如某个系统多个模块,我们把资产管理模块和车辆管理模块,分开,在总的struts.xml配置文件中include他们: 工程结构: struts. ...
- struts2学习(1)struts2 helloWorld
一.struts2简介: 二.helloWorld: 1)工程结构: HelloWorldAction.java: package com.cy.action; import com.opensymp ...
- Struts2学习笔记——Struts2搭建和第一个小程序
1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...
- Struts2学习笔记--Struts2的体系结构
1. Struts2体系结构 Struts是以前端控制器框架为主体的框架,用户的请求会通过控制器选择不同的Action类来执行具体的操作,在Action类中所有的Servlet对象(request.r ...
- struts2学习(15)struts2防重复提交
一.重复提交的例子: 模拟一种情况,存在延时啊,系统比较繁忙啊啥的. 模拟延迟5s钟,用户点了一次提交,又点了一次提交,例子中模拟这种情况: 这样会造成重复提交: com.cy.action.St ...
- struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载
四.多个文件上传: 五.struts2文件下载: 多个文件上传action com.cy.action.FilesUploadAction.java: package com.cy.action; i ...
随机推荐
- a标签 在新页面打开
<a href="https://www.baidu.com/" target="_blank">下载</a>
- <<网络是怎样连接的>>笔记第2章用电信号传输Tcp/ip数据
创建套接字: 协议栈的内部结构.套接字的实体,以及创建套接字的操作过程 连接服务器: “连接”具体是进行怎样的操作,在这个过程中协议栈到底是如何工作的, 以及客户端和服务器是如何进行交互的. 收发数据 ...
- IOS-组件化架构漫谈
本文作者: 伯乐在线 - 刘小壮 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 前段时间公司项目打算重构,准确来说应该是按之前的产品逻辑重写一个项目
- Jenkins无法读取覆盖率报告的解决方法
报错信息如下: log 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 coverage-report: [mkdir] Cre ...
- WIN8.1 PRO RTM VOL.2013.09.18
文件名:cn_windows_8_1_pro_vl_x64_dvd_2791218.isoSHA1:61C002551763E22B64EB1BACEFFE83620114C3D6 文件名:cn_wi ...
- 古董留念 - Microsoft Office 4.2中文版
Office 4.2是Office 95的前一个版本,最适合运行在Windows 3.x上,但即使是最新的Windows 7 32位版也是可以安装它的(不信你可以试试)! 原版以软盘为载体,安装一次需 ...
- 201621123005《java程序设计》第八次实验总结
201621123005<Java程序设计>第八次实验总结 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1 ...
- WebLogic发布S2SH应用时提示ClassNotFoundException: org.hibernate.hql.ast.HqlToken异常
使用Spring+hibernate如下 <properties> <!--定义方言.fetch深度.是否显示sql--> <property name="hi ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 11
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...
- PHP ksort() 函数
PHP ksort() 函数 PHP Array 函数 实例 按照键名对关联数组进行升序排序: <?php $age=array("Bill"=>"60&qu ...