深入Struts2学习(三)
来源:互联网 发布:网络十大公会 编辑:程序博客网 时间:2024/05/21 14:44
指定多个配置文件
在实际项目会遇到很多模块,没个模块都会有相应的action,如果每个模块的action都配置在struts.xml就会很难维护,这时就用到了指定多个配置文件。
<include file="login.xml"></include><include file="system.xml"></include>struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <!-- 同一路径下 --><include file="helloworld.xml"></include><constant name="struts.enable.DynamicMethodInvocation" value="false"></constant></struts>helloworld.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <package name="default" namespace="/" extends="struts-default"><!-- 3、通配符方式,官方推荐,struts.enable.DynamicMethodInvocation改为false 在action属性中增加method="{1}",同时name值改为helloworld_* 访问为:http://localhost:8080/HelloStruts2_1/helloworld_update3.action *匹配{1},如果 helloworld_*_*,method="{1}{2}" 如果name="*_*" method="{2}" class=com.lijy.action.{1}Action 第一个*匹配{1},第二个*匹配方法名 访问为http://localhost:8080/HelloStruts2_1/Hello_update3.action 也可以通配包名如class=class="com.lijy.{3}.action.{1}Action --> <action name="helloworld_*" method="{1}" class="com.lijy.action.HelloAction"> <result>/reult.jsp</result> <!-- 2、感叹号方式,官方不推荐 需要配置struts.enable.DynamicMethodInvocation为true http://localhost:8080/HelloStruts2_1/helloworld!add2.action --> <result name="add2">/add2.jsp</result> <result name="update2">/update2.jsp</result> <!-- 3、通配符方式 --> <result name="add3">/{1}.jsp</result> <result name="update3">/{1}.jsp</result> <result name="add3">/{2}.jsp</result> <result name="update3">/{2}.jsp</result> </action> <!-- 动态方法调用三种方式 1、指定method属性 ,即一个方法对应一个action--> <action name="addAction" method="add" class="com.lijy.action.HelloAction"> <result>/add.jsp</result> </action> <action name="updateAction" method="update" class="com.lijy.action.HelloAction"> <result>/update.jsp</result> </action> </package><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant></struts>
默认Action
当用户访问的url不存在时,配置相应的默认action。在helloworld.xml中增加如下:
<default-action-ref name="index"></default-action-ref><action name="index"><result>/error.jsp</result></action>
接收参数
使用action属性接收参数
创建表单
<form action="com/lijy/action/LoginAction.action" method="post">用户名:<input type="text" name="username" />密码:<input type="password" name="password"/><input type="submit" value="提交" /></form>创建login-struts.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="login" namespace="/" extends="struts-default"><action name="LoginAction" method="login" class="com.lijy.action.LoginAction"><result>/success.jsp</result></action></package></struts>package中的name用于区分package
在struts.xml中引入
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><include file="helloworld.xml"></include><include file="login-struts.xml"></include><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant></struts>创建action
package com.lijy.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String username;private String password;public String login() { System.out.println(username); return SUCCESS;}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;}}如果页面有上百个属性,就要在action中配置相应的属性,会很麻烦,不利于维护。如果把属性放到对象中就更利于维护了。
使用Domain Model接收参数
修改表单为:
<form action="LoginAction2.action" method="post">用户名:<input type="text" name="user.username" />密码:<input type="password" name="user.password"/><input type="submit" value="提交" /></form>创建用户对象
package com.lijy.bean;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;}}
login-struts2.xml
<package name="login2" namespace="/" extends="struts-default"><action name="LoginAction2" method="login" class="com.lijy.action.LoginAction2"><result>/success.jsp</result></action></package>
package com.lijy.action;import com.lijy.bean.User;import com.opensymphony.xwork2.ActionSupport;public class LoginAction2 extends ActionSupport { private User user;public String login() {System.out.println(user.getUsername());return SUCCESS;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
使用ModelDriven接收参数
修改表单为
<form action="LoginAction3.action" method="post">用户名:<input type="text" name="username" />密码:<input type="password" name="password"/><input type="submit" value="提交" /></form>修改action,实现ModelDriven接口
package com.lijy.action;import com.lijy.bean.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class LoginAction3 extends ActionSupport implements ModelDriven<User> { private User user = new User();public String login() {System.out.println(user.getUsername());return SUCCESS;}@Overridepublic User getModel() {return user;}}login-struts3.xml
<package name="login3" namespace="/" extends="struts-default"><action name="LoginAction3" method="login" class="com.lijy.action.LoginAction3"><result>/success.jsp</result></action></package>
如果传入的为数据集--List集合
用户对象(User.java)增加两个List
private List<String> bookList;private List<User> userList;并实现get和set方法,此时的action中的login方法可以修改为
public String login() {System.out.println(user.getUsername());System.out.println(user.getBookList().get(0));System.out.println(user.getBookList().get(1));System.out.println(user.getUserList().get(0).getUsername());System.out.println(user.getUserList().get(1).getUsername());return SUCCESS;}form表单修改为:
<form action="LoginAction3.action" method="post">用户名:<input type="text" name="username" />密码:<input type="password" name="password"/>书籍1:<input type="text" name="bookList[0]"/>书籍2:<input type="text" name="bookList[1]"/>用户1:<input type="text" name="userList[0].username"/>用户2:<input type="text" name="userList[1].username"/><input type="submit" value="提交" /></form>
阅读全文
0 0
- 深入Struts2学习(三)
- 深入struts2(三)
- (三)深入了解Struts2
- 深入Struts2学习(二)
- 深入Struts2学习(四)
- Struts2学习笔记(三)
- struts2-学习笔记(三)
- Struts2学习(三)上传下载
- Struts2学习笔记(三)
- Struts2学习笔记(三)
- MemCache深入学习(三)
- Struts2 标签详解(学习深入)
- Struts2系统学习(3)Action深入
- struts2的深入学习(一)
- 深入学习Struts2
- 深入学习Struts2
- Struts2 深入学习(待续)
- 深入学习struts2
- [SDOI2010]魔法猪学院(Astar 优化K短路)
- 算法总结JS版(一)—— 冒泡排序(Bubble Sort)
- 用C实现C编译器(一)
- Filter(第四节)
- Junit单元测试避免死循环
- 深入Struts2学习(三)
- 基于Java和websocket的在线聊天程序(可群发和选择用户)
- Binary Search:34. Search for a Range
- C语言中的宏
- 数组中的strlen和sizeof
- GIT的安装及上传代码到码云
- 白话解析:一致性哈希算法 consistent hashing
- 本文作者YY硕,来自大疆工程师《机器人工程师学习计划》
- 从零开始学电脑 3.1