struts1.3.10_demo

一、添加Jar包【lib下所有】

二、配置

1、在web.xml中配置ActionServlet

      <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>2</load-on-startup>  //是基础,要先创建
	</servlet>
	
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

2、配置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="dynaActionForm" 
							    type="org.apache.struts.action.DynaActionForm">
			<form-property name="username" type="java.lang.String"></form-property>
			<form-property name="password" type="java.lang.String"></form-property>
		</form-bean>
	</form-beans>
	
	<global-exceptions>
		<exception type="com.song.exception.GlobalException" key="errors" path="/error.jsp"></exception>
	</global-exceptions>

	<action-mappings>
		<action path="/user"
					   type="com.song.action.UserAction" 
					   name="dynaActionForm"
					   parameter="command">
			<forward name="success" path="/ok.jsp"></forward>
			<!-- 可以加redirect="true"改为重定向 -->
			<forward name="failed" path="/error.jsp"></forward>
		</action>
		
		<action path="/changeLan"
					   type="com.song.action.ChangeLanAction">
		</action>
	</action-mappings>
	
	<message-resources parameter="MessageResource"></message-resources>
	
</struts-config>


3、添加国际化资源文件,放在classpath下,名称格式:MessageResource_en_US.properties,例如:

login.username=username
login.password=password
login.submit=submit
errors={0}
ok={0} login success

4、示例中的两个Action:

①UserAction:

package com.song.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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.song.service.UserService;

public class UserAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm daf = (DynaActionForm)form;
		String username = daf.getString("username");
		String password = daf.getString("password");
		String result = new UserService().addUser(username,password);
		
		//添加ActionMesssages
		ActionMessages msgs = new ActionMessages();
		ActionMessage msg = new ActionMessage("ok", username);
		msgs.add("ok", msg);
		this.addMessages(request, msgs);
		
		return mapping.findForward(result);
	}

}
②ChangeLanAction:

package com.song.action;

import java.util.Locale;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChangeLanAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String language = request.getParameter("lan");
		Locale locale = new Locale(language);
		
		this.setLocale(request, locale);
		ActionForward actionForward = new ActionForward();
		actionForward.setPath("/index.jsp");
		return actionForward;
	}

}

5.示例中的三个页面:

①index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
  <a href="changeLan.do?lan=zh">中文</a>
  <a href="changeLan.do?lan=en">English</a>
    <form action="user.do?command=add" method="post">
    	<bean:message key="login.username" />:<input type="text" name="username"><br>
    	<bean:message key="login.password" />:<input type="text" name="password"><br>
    	<input type="submit" value="<bean:message key="login.submit" />">
    </form>
  </body>
</html>
②ok.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!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>
<html:messages id="i" property="ok" message="true">
	${i }
</html:messages>
</body>
</html>
③error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!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>
<html:errors/>
</body>
</html>

你可能感兴趣的:(struts1.3.10_demo)