struts1配置

一個簡單的login登錄界面,這也有問題,那也有問題,真是揪心,由於是手動配置,所以很多細節方面都出現了問題,就自己所犯的錯誤,總結經驗:

1index.xml文件放在WebRoot的根目錄下,如果放在WEB-INF下,即使web.xml中配置了路徑加名稱也不行。


2:在輸入界面的action中,輸入的是對應的動作,要加上工程名稱:

Action=”/Login/login.do

相同顏色,對應相同關係

3:<form-beanname="userform"type="com.ypj.form.UserForm">

<action path="/login" name="userform"type="com.ypj.action.LoginAction">


4:處理亂碼問題需要配置過濾器在web.xml,還有過濾器類:

要放在開始的地方,最前面

<filter>

<filter-name>setEncoding</filter-name>

<filter-class>com.ypj.Filter.FilterEncoding</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>setEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


packagecom.ypj.Filter;


importjava.io.IOException;


importjavax.servlet.Filte;

importjavax.servlet.FilterChain;

importjavax.servlet.FilterConfig;

importjavax.servlet.ServletConfig;

importjavax.servlet.ServletException;

importjavax.servlet.ServletRequest;

importjavax.servlet.ServletResponse;;



publicclass FilterEncoding implements Filter {

private FilterConfig filterConfig = null;

private String encoding = null;

@Override

public void destroy() {

// TODO Auto-generated methodstub

this.encoding = null;

this.filterConfig = null;

}

public void init(ServletConfig arg0)throws ServletException {

// TODO Auto-generated methodstub

this.filterConfig = filterConfig ;

this.encoding =filterConfig.getInitParameter("encoding");

}


@Override

public void doFilter(ServletRequestrequest, ServletResponse response,

FilterChain chain)throws IOException, ServletException {

// TODO Auto-generated methodstub

String encoding =this.encoding;

if(encoding != null)

{

request.setCharacterEncoding(encoding);

}

chain.doFilter(request,response);

}


@Override

public void init(FilterConfigfilterConfig) throws ServletException {

// TODO Auto-generated methodstub

this.filterConfig =filterConfig;

this.encoding =filterConfig.getInitParameter("encoding");

}


}


5:在函數裏面點擊alt+/可以選擇函數中自帶的方法,若出現錯誤,把鼠標放到出現紅色波浪線的上面,會提示需要導入包或者別的動作,單擊即可。


6Action 中是方法,執行的動作,ActionForm 是表單


7:配置servlet

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>

包的路徑

org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value> 注意路徑

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章