struts2配置,struts2
一、先匯入jar包
先解壓個空白的項目,從裡面複製jar包
二、導完jar包後,需要配置struts.xml檔案和web.xml檔案
1.匯入web.xml檔案,改配置.
相當於加了一個struts過濾器
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
2.加入struts.xml檔案到src目錄下
struts.xml檔案配置, 主要配置的是action
<?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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 對應著IndexAction裡面的execute方法 --> <action name="index" class="com.itnba.maya.controller.IndexAction"> <!-- execute方法return SUCCESS時不用寫name --> <result> index.jsp <!--跳轉的頁面 --> </result> <result name="error"> index_error.jsp </result> <result name="haha"> index_haha.jsp </result> </action> <!-- 直接跳轉不用處理資料時 不需要寫class --> <action name="home"> <result> home.jsp </result> </action> </package> </struts>
3.建一個類,繼承com.opensymphony.xwork2.ActionSupport
在該類中,重寫execute()方法
成員變數用來相互傳值,產生get set
package com.itnba.maya.controller;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String execute() throws Exception { msg+="haha"; return SUCCESS; } }
這樣就簡單的配置完成了,實際運用還要根據情況修改