struts2配置

引入相关jar包之后进行如下配置:
1、web.xml


<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>

 
  <filter>
    <filter-name>Struts2filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    filter-class>
  filter>

  <filter-mapping>
    <filter-name>Struts2filter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>  

web-app>

2、系统会默认搜索struts.xml,因此action在该文件中配置
struts.xml文件配置:



<struts>

    
    
    <include file="struts-common.xml"/>
    
    <include file="struts-cost.xml"/>
    
    <include file="struts-login.xml"/>

struts>

sturts-common.xml:(定义公共请求部分,如拦截器)



<struts>

    
    <package name="netctoss" extends="json-default">
        
        <interceptors>
            
            <interceptor name="loginInter" 
                class="com.netctoss.interceptor.LoginInterceptor"/>
            
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInter"/>
                <interceptor-ref name="defaultStack"/>
            interceptor-stack>
        interceptors>

        
        <default-interceptor-ref name="loginStack"/>

        
        <global-results>
            <result name="login" type="redirectAction">
                <param name="namespace">/loginparam>
                <param name="actionName">toLoginparam>
            result>
            <result name="error">
                /WEB-INF/main/error.jsp
            result>
        global-results>       
    package>

struts>

struts-cost.xml:(业务部分action配置)



<struts>

    
    <package name="cost" namespace="/cost"
        extends="netctoss">
        
        
        <action name="findCost"
            class="com.netctoss.action.cost.FindCostAction">
            <param name="pageSize">4param>
            <result name="success">
                /WEB-INF/cost/findCost.jsp
            result>
            <result name="error">
                /WEB-INF/cost/error.jsp
            result>
        action>
        
        <action name="deleteCost"
            class="com.netctoss.action.cost.DeleteCostAction">
            <result name="success" type="redirectAction">
                findCost
            result>
        action>
        
        <action name="toAddCost">
            <result name="success">
                /WEB-INF/cost/addCost.jsp
            result>
        action>
        
        <action name="addCost"
            class="com.netctoss.action.cost.AddCostAction">
            <result name="success" type="redirectAction">
                findCost
            result>
        action>
        
        <action name="checkCostName"
            class="com.netctoss.action.cost.CheckCostNameAction">
            <result name="success" type="json">
                <param name="root">infoparam>
            result>
        action>
        
        <action name="toUpdateCost"
            class="com.netctoss.action.cost.ToUpdateCostAction">
            <result name="success">
                /WEB-INF/cost/updateCost.jsp
            result>
        action>
        
        <action name="updateCost"
            class="com.netctoss.action.cost.UpdateCostAction">
            <result name="success" type="redirectAction">
                findCost
            result>
        action>
    package>

struts>

上面result type为json时部分代码,定义为map类型,返回struts2自动转成了json类型:

private Map info =
        new HashMap();

    public String execute() {
        ICostDAO dao = DAOFactory.getCostDAO();
        try {
            Cost cost = dao.findByName(name);
            if(cost == null) {
                //没查到,说明没重名,验证通过
                info.put("pass", true);
                info.put("message", "资费名有效.");
            } else {
                //查到了,说明重名了,验证失败
                info.put("pass", false);
                info.put("message", "资费名重复.");
            }
        } catch (DAOException e) {
            e.printStackTrace();
            //发生异常时,给予错误的提示
            info.put("pass", false);
            info.put("message", "系统异常,请联系系统管理员.");
        }
        return "success";
    }

struts-login.xml:(登录action)



<struts>

    
    <package name="login" namespace="/login"
        extends="struts-default">
        
        <action name="toLogin">
            <result name="success">
                /WEB-INF/main/login.jsp
            result>
        action>
        
        <action name="login"
            class="com.netctoss.action.login.LoginAction">
            <result name="success">
                /WEB-INF/main/index.jsp
            result>
            <result name="fail">
                /WEB-INF/main/login.jsp
            result>
            <result name="error">
                /WEB-INF/main/error.jsp
            result>
        action>
        
        <action name="createImage"
            class="com.netctoss.action.login.CreateImageAction">
            <result name="success" type="stream">
                <param name="inputName">
                    imageStream
                param>
            result>
        action>
    package>

struts>

你可能感兴趣的:(Struts2,struts2.0)