java之struts2的配置讲解(2)


java之struts框架入门教程

基础上,进行下列操作

1.结构对比

原来的项目结构图

现在的结构图

即从结构上可以看出,在HelloStruts项目中增加了config 文件夹(Source Folder) 及user.xml 文件

2.修改配置文件,使struts.xml 中包含 user.xml 配置文件

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>
    
    
    
    <constant name="struts.i18n.encoding" value="utf-8"/>
    
    <constant name="struts.action.extension" value="do,action,,zhangsan"/>
    
    <constant name="struts.devMode" value="true"/>
    
    
    <include file="cn/qm/struts/user.xml">include>


    
struts>

user.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" extends="struts-default" namespace="/sys">
    
        
        <action name="hello" class="cn.qm.action.HelloAction" method="hello">
            
            <result name="success" type="dispatcher">/index.jspresult>
        action>
    package>
    
    
struts>

3.修改 HelloAction 类,增加 hello 方法

public class HelloAction {

    ////struts2的处理方法 都是 public String的  默认执行execute,并且处理方法没有参数
    public String execute(){
        System.out.println("请求被接收了...");
        return "success";
    }
    
    public String hello(){
        System.out.println("hello");
        return Action.SUCCESS;
    }
}

因为在配置文件中,指定了hello方法,所以请求会进入hello方法

4.运行程序,并且在浏览器输入网址验证

网址:http://localhost:8080/Hello/sys/hello.action

浏览器显示

myeclipse中的console显示

说明请求成功被接收了。

相关