Struts入门—基本配置

1、导入jar包

2、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "dtd\web-app_2_3.dtd">

<web-app>
  <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>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>    
  </welcome-file-list>
</web-app>

3、配置struts-config.xml文件,放在WEB-INF目录下

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
	<form-beans>
		<form-bean name="userinfo" type="test.UserInfoBean"/>
	</form-beans>

  <global-forwards>
      <forward name="success" path="/test/success.jsp" redirect="false" />
  </global-forwards>

  <action-mappings>   
    <action path="/login" type="test.TestAction" name="userinfo"/> 
 </action-mappings>

</struts-config>

4、登录页面

<form id="userinfo" name="userinfo" action="<%=request.getContextPath()%>/login.do" method="post">
	用户名:<input id="username" type="text" name="userName"/><br/>
	密  码:<input id="password" type="password" name="password"/><br/>
	<input id="submit" type="submit" value="登 录"/>
</form>

5、Action:继承org.apache.struts.action.Action,重写execute()方法

6、Bean需要继承org.apache.struts.action.ActionForm

相关推荐