Struts1入門級程式hello world(解決struts1中文亂碼)

JQprince發表於2018-07-13

Struts1入門之hello world

    這是我最近學習struts1後,做的一個小demo,在此整理和總結一下。

IDE工具:eclipse

準備工作:

建專案:新建 Dynamic Web Project

導包:引入struts1對應的jar包到lib目錄下

建立package:action 和 form

建立class:在action包下建立HelloWorldAction 和 在form包下建立HelloWorldForm

建立xml配置檔案:在WEB-INF下建立struts-config.xml 和web.xml

建立jsp:在WebContent下建立index.jsp 和 helloWorld.jsp

如圖所示:


程式碼如下:

HelloWorldAction:

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import form.HelloWorldForm;

public class HelloWorldAction extends DispatchAction{
    
    public ActionForward toHello(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        HelloWorldForm hf = (HelloWorldForm)form;
        String m = hf.getMsg();
        request.setAttribute("msg", m);
        return mapping.findForward("hello");
    }
}

HelloWorldForm:

package form;

import org.apache.struts.action.ActionForm;

public class HelloWorldForm extends ActionForm{
    
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  	<form-beans>
        <form-bean name="helloWorldForm" type="form.HelloWorldForm" />
    </form-beans>
    <action-mappings>
       <action  path="/helloWorld" name="helloWorldForm"  type="action.HelloWorldAction" scope="request" 
       parameter="method">
         <forward name="hello" path="/helloWorld.jsp" />
       </action>
    </action-mappings>
</struts-config>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>helloworld</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
	<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>
	    <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	    <servlet-name>action</servlet-name>
	    <url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>呼叫action方法跳轉到helloWorld.jsp</h1>
    <form action="helloWorld.do?method=toHello" method="post">
        輸入傳遞資訊:<input type="text" name="msg"/>
        <input type="submit" value="傳送"/>
    </form>
</body>
</html>

helloWorld.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>hello world</h1>
    <!-- 使用bean標籤輸出上個頁面傳過來的name屬性的值 -->
    <h5>bean標籤輸出:<bean:write name="helloWorldForm" property="msg"/></h5>
    
    <!-- 也可以直接用${msg}輸出 -->
    <h5>EL表示式輸出:${msg}</h5>
</body>
</html>

到此時struts1的hello world小demo就完成了。

不過傳值的時候會發生中文亂碼,解決方法就是建立一個過濾器攔截,並設定編碼格式為UTF-8即可。

修改web.xml:在其中新增一個過濾器,放在<web-app></web-app>標籤中間

<!-- 設定過濾器解決中文亂碼 -->
	<filter>
	<filter-name>encoding</filter-name>
	<filter-class>filter.EncodingFilter</filter-class>
	</filter>
	<filter-mapping>
	<filter-name>encoding</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>

建立一個filter包,其中新建EncodingFilter類:

package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		arg0.setCharacterEncoding("UTF-8");
		arg2.doFilter(arg0, arg1);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}


成功執行此demo時,說明你已經入門了struts1。

相關文章