JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理

 

 

 

  前导博文

    JavaWeb_(SSH)使用Struts框架实现用户的登陆  传送门

    JavaWeb_(SSH)Struts创建Action的三种方式   传送门

 

 

  核心配置

  动态方法调用

  结果集处理

 

一、核心配置

  struts.xml

    <!-- name:配置包名 namespace:给action的访问路径定义一个命名空间 -->
    <package name="MyPackage" namespace="/user" extends="struts-default">
        <!-- action:配置action类
            name:决定了action访问的资源名称  servlet:url-pattern 
            class:action的完整类名
            method:指定调用action中的哪个方法来去处理请求-->
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默认为转发  redirect设置为重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>

 

  namespace:作用是可以让不同的packet里面包含相同action名称,起虚拟路径作用

<package name="MyPackage" namespace="/" extends="struts-default"></package>

  此时访问的路径http://localhost:8080/项目名字/请求

 

    <package name="MyPackage" namespace="/user" extends="struts-default">

  此时访问路径此时访问的路径http://localhost:8080/项目名字/user/请求

 

 

二、动态方法调用

  在web层UserAction.java类中编写login()注册方法

        <action name="LoginAction" class="com.Gary.web.UserAction" method="login">
            <!-- 默认为转发  redirect设置为重定向  -->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>

 

public String login() throws Exception {
        
        System.err.println("login()方法");
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }

  

  在web层UserAction.java类中编写register()注册方法

        <action name="LoginActionRegister" class="com.Gary.web.UserAction" method="register">
            <!-- 默认为转发  redirect设置为重定向  --> 
            <result name ="success" type="redirect">/index.html</result>
            <result name ="error">/login.jsp</result>
        </action>

 

    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

 

  此时,如果想要访问com.Gary.web.UserAction路径下的login方法,就需要调用http://localhost:8080/StrutsForum_Login/LoginAction,如果想要访问com.Gary.web.UserAction路径下的register方法,就需要调用http://localhost:8080/StrutsForum_Login/LoginActionRegister。

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

    public User user = new User();
    
    public String login() throws Exception {
        
        System.err.println("login()方法");
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
            
    }
    
    //注册
    public String register() throws Exception {
        
        System.err.println("register()方法");
        
        return null;
    }

    
    
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
UserAction.java

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
    
    
<struts>
    <!-- name:配置包名 namespace:给action的访问路径定义一个命名空间 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
    
        <!-- action:配置action类
            name:决定了action访问的资源名称  servlet:url-pattern 
            class:action的完整类名
            method:指定调用action中的哪个方法来去处理请求  -->
            
        <action name="LoginAction" class="com.Gary.web.UserAction" method="login">
            <!-- 默认为转发  redirect设置为重定向  -->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
        
        <action name="LoginActionRegister" class="com.Gary.web.UserAction" method="register">
            <!-- 默认为转发  redirect设置为重定向  --> 
            <result name ="success" type="redirect">/index.html</result>
            <result name ="error">/login.jsp</result>
        </action>

        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        </action>
        
        <action name="LoginActionImpl" class="com.Gary.web.ImplAction" method="execute">
        
        </action>
    </package>
</struts>
struts.xml

 

  如果每一个方法就需要在struct.xml中配置一个<action>,那配置多个方法时就有一些麻烦了,此时我们可以用到struct框架中的动态方法调用

  使用动态方法调用一定注意在struct.xml中添加两行<constant>标签和一行<global-allowed-methods>

<constant name="struts.devMode" value="true"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<!-- 增加动态方法调用的安全性 -->
<global-allowed-methods>login,register,kill</global-allowed-methods>

 

 

三、结果集

 

            
        <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
            <!-- 默认为转发  redirect设置为重定向  -->
            <result name="success" type="redirect">/index.html</result>
            <!-- 默认为转发 -->
            <result name="error">/login.jsp</result>
        </action>

 

  转发到Action【转发的路径是不会变的】

  在struct.xml中配置动态方法调用

        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        
        </action>
        
        <action name="LoginActionImpl" class="com.Gary.web.ImplAction" method="execute">
            <!-- 转发到LoginActionDefault -->
            <result name="defaultAction" type="chain">LoginActionDefault</result>
        </action>

 

  ImplAction.java中编写execute()方法

    @Override
    public String execute() throws Exception {
        System.out.println("这是实现了Action接口的action");
        return "defaultAction";
    }

 

 

package com.Gary.web;

import com.opensymphony.xwork2.Action;

public class ImplAction implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("这是实现了Action接口的action");
        return "defaultAction";
    }

    
    
    
}
ImplAction.java

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
    
    
<struts>

    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    <!-- name:配置包名 namespace:给action的访问路径定义一个命名空间 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <!-- 增加动态方法调用的安全性 -->
        <global-allowed-methods>login,register,kill</global-allowed-methods>
    
        <!-- action:配置action类
            name:决定了action访问的资源名称  servlet:url-pattern 
            class:action的完整类名
            method:指定调用action中的哪个方法来去处理请求  -->
            
        <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
            <!-- 默认为转发  redirect设置为重定向  -->
            <result name="success" type="redirect">/index.html</result>
            <!-- 默认为转发 -->
            <result name="error">/login.jsp</result>
        </action>


        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        
        </action>
        
        <action name="LoginActionImpl" class="com.Gary.web.ImplAction" method="execute">
            <!-- 转发到LoginActionDefault -->
            <result name="defaultAction" type="chain">LoginActionDefault</result>
        </action>
    </package>
</struts>
struts.xml

 

  重定向到Action【转发的路径是不会变的】

  在struts.xml中添加"toLogin"结果集,配置type="redirectAction"

<action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
            <!-- 转发到LoginActionDefault -->
            <result name="defaultAction" type="chain">LoginActionDefault</result>
            <!-- 重定向到Action(LoginAction_*) -->
            <result name="toLogin" type="redirectAction">
                <param name="actionName">LoginAction_login</param>
                
                <param name="username">${username}</param>
                <param name="password">${password}</param>
            </result>
        </action>

 

  在ImplAction.java中添加login()方法,return去发起toLogin请求

    public String login()
    {
        //得到原生的request域
        //ServletActionContext.getRequest().setAttribute("username", "123");
        //ServletActionContext.getRequest().setAttribute("password", "123");
        
        ActionContext.getContext().put("username", "123");
        ActionContext.getContext().put("password", "123");
        
        return "toLogin";
    }

 

  运行程序,在网页的url中输入http://localhost:8080/StrutsForum_Login/LoginActionImpl_login请求,请求后【http://localhost:8080/StrutsForum_Login/LoginAction_login路径不变

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class ImplAction implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("这是实现了Action接口的action");
        return "defaultAction";
    }

    
    public String login()
    {
        //得到原生的request域
        //ServletActionContext.getRequest().setAttribute("username", "123");
        //ServletActionContext.getRequest().setAttribute("password", "123");
        
        ActionContext.getContext().put("username", "123");
        ActionContext.getContext().put("password", "123");
        
        return "toLogin";
    }
    
    
}
ImplAction.java

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
    
    
<struts>

    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    <!-- name:配置包名 namespace:给action的访问路径定义一个命名空间 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <!-- 增加动态方法调用的安全性 -->
        <global-allowed-methods>regex:.*</global-allowed-methods>
    
        <!-- action:配置action类
            name:决定了action访问的资源名称  servlet:url-pattern 
            class:action的完整类名
            method:指定调用action中的哪个方法来去处理请求  -->
            
        <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}">
            <!-- 默认为转发  redirect设置为重定向  -->
            <result name="success" type="redirect">/index.html</result>
            <!-- 默认为转发 -->
            <result name="error">/login.jsp</result>
        </action>


        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        
        </action>
        
        <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}">
            <!-- 转发到LoginActionDefault -->
            <result name="defaultAction" type="chain">LoginActionDefault</result>
            <!-- 重定向到Action(LoginAction_*) -->
            <result name="toLogin" type="redirectAction">
                <param name="actionName">LoginAction_login</param>
                
                <param name="username">${username}</param>
                <param name="password">${password}</param>
            </result>
        </action>
    </package>
</struts>
struts.xml

 

转载于:https://www.cnblogs.com/1138720556Gary/p/10480086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值