在web.xml中声明初始化spring容器的监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在web.xml中声明struts1的控制器
<servlet>
<servlet-name>struts</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>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="personForm" type="cn.itcast.web.formbean.PersonForm"/>
</form-beans>
<action-mappings>
<action path="/person/list" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp"/>
</action>
<action path="/person/manage" parameter="method"
validate="false" scope="request" name="personForm">
<forward name="message" path="/WEB-INF/page/message.jsp"/>
</action>
</action-mappings>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
</struts-config>
beans.xml中定义action
<bean name="/person/list" class="cn.itcast.web.action.PersonAction"/>
<bean name="/person/manage" class="cn.itcast.web.action.PersonManageAction"/>
action中通过注解将service注入
public class PersonAction extends Action {
@Resource PersonService personService;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {/*
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
this.getServlet().getServletContext());
PersonService personService =(PersonService)ctx.getBean("personService");*/
request.setAttribute("persons", personService.getPersons());
return mapping.findForward("list");
}
}
使用过滤器解决乱码问题
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>