Struts的页面控制与配置

一,用到文件
1.输入页面
input1.jsp页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
	<head>
		<title>JSP for HelloWorldForm form</title>
	</head>
	<body>
		<html:form action="/helloWorld2">
			msg : <html:text property="msg"/><html:errors property="msg"/><br/>
			<html:submit/><html:cancel/>
		</html:form>
	</body>
</html>


threeInput.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'input3.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="<%=basePath%>threeInput.do" method="post">&nbsp; 
    欢迎语:<input type="text" name="msg" value="" />
    <input type="submit" name="method" value="insert" />
    <input type="submit" name="method" value="update" />
    <input type="submit" name="method" value="delete" />
    </form>
  </body>
</html>


2.输出页面
show.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'show.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <%
  String str = (String)request.getAttribute("helloWorld");
  %>
  <body>
   你输入的欢迎语1是:${helloWorld}<br>
   你输入的欢迎语2是:<%=str%>
  </body>
</html>


3.ActionForm类

public class HelloWorldForm extends ActionForm {
	/*
	 * Generated fields
	 */

	/** msg property */
	private String msg;

	/*
	 * Generated Methods
	 */

	/** 
	 * Method reset
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
	}

	/** 
	 * Returns the msg.
	 * @return String
	 */
	public String getMsg() {
		return msg;
	}

	/** 
	 * Set the msg.
	 * @param msg The msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}
}


4.action类

[1]HelloWorld1Action 类
public class HelloWorld1Action extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String str = "hello zhao在";
		request.setAttribute("helloWorld", str);
		return mapping.findForward("show");
	}
}

[2]HelloWorld2Action 类
public class HelloWorld2Action extends Action {
			
public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HelloWordForm helloWordForm = (HelloWordForm) form;// TODO Auto-generated method stub
		String msg = helloWordForm.getMsg();
		request.setAttribute("helloWorld", msg);
		//response.setCharacterEncoding("utf-8");
		//response.setContentType("text/html;charset=utf-8");
		return mapping.findForward("show");
	}
}

[3]ThreeInputAction类
public class ThreeInputAction extends DispatchAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HelloWordForm helloWordForm = (HelloWordForm) form;// TODO Auto-generated method stub
		return null;
	}
}

二.配置struts-config.xml
1.无输入页面,只有action和输出
<action-mappings >
    <action path="/helloWorld1" type="com.zhao.struts.action.HelloWorld1Action">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>
</struts-config>

2.有输入页面,无bean,action就要用 request.getParameter("msg");拿参数
3.有bean
<form-beans >
    <form-bean name="helloWorldForm" type="com.zhao.struts.form.HelloWorldForm" />
</form-beans>

<action-mappings >
 <action
      attribute="helloWorldForm"
      name="helloWorldForm"
      path="/helloWorld2"
      scope="request"
      type="com.zhao.struts.action.HelloWorld2Action"
      validate="false">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>
</action-mappings >

4.输入页面的配置
[1]直接/form/input1.jsp浏览就不用配置
[2]
<!--struts能生成-->
 <action forward="/WEB-INF/jsp/input1.jsp" path="/input2" />
 <action include="/WEB-INF/jsp/input1.jsp" path="/input3" />
 <!--struts不能生成-->   
     <action 
    path="/input4" 
    type="org.apache.struts.actions.ForwardAction"
    parameter="/WEB-INF/jsp/input1.jsp" />

5.多按钮的配置
<action
      attribute="helloWorldForm"
      name="helloWorldForm"
      parameter="method"
      path="/threeInput"
      scope="request"
      type="com.zhao.struts.action.ThreeInputAction">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>

你可能感兴趣的:(java,jsp,bean,xml,struts)