深入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>
修改action
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>










原创粉丝点击