第六步:建立JSP视图页面 (主要有两个jsp文件. 添加页面. adduser.jsp 查看页面 viewuser.jsp)
adduser.jsp
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=gb2312" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>
adduser
</title>
</head>
<body>
<h1> </h1>
<p>
<html:form action="/user1Action.do" method="POST">
<html:hidden property="action"/>
<br>
部门:
<html:select property="dep">
<html:options collection="dep" property="id" labelProperty="name1"/>
</html:select>
<br>
ID:
<logic:equal name="userActionForm" property="action" scope="request" value="add">
<html:text property="user_id"/>
</logic:equal>
<logic:equal name="userActionForm" property="action" scope="request" value="edit">
<html:text property="user_id" readonly="true"/>
</logic:equal>
<br>
名字:<html:text property="user_name"/>
<br>
电话:<html:text property="tel"/>
<br>
<html:submit property="submit" value="Submit"/><br>
<html:reset value ="Reset"/>
</html:form>
</body>
</html:html>
viewuser.jsp
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" %>
<html:html>
<head>
<title>
viewuser
</title>
</head>
<body>
<div align="center"><a href="userAction.do?action=add">add </a></div>
<table width="60%" border="1" align="center">
<tr bgcolor="#CCCCCC">
<td>ID</td>
<td>user_name</td>
<td>dep</td>
<td>tel</td>
<td>modify</td>
<td>del</td>
</tr>
<logic:iterate name="rs" id="user" scope="request" type="java.util.HashMap">
<tr>
<td><bean:write name="user" property="user_id"/> </td>
<td><bean:write name="user" property="user_name"/> </td>
<td><bean:write name="user" property="dep_name"/> </td>
<td><bean:write name="user" property="tel"/> </td>
<td><a href="userAction.do?action=edit&key=<bean:write name="user" property="user_id"/>">modify</a></td>
<td><a href="userAction.do?action=del&key=<bean:write name="user" property="user_id"/>">del</a></td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>
建产一个转向页面 (index.jsp)
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<% response.sendRedirect("userAction.do?action=view"); %>
</body>
</html>
递七步:建立struts配置文件 (WEB-INF/config-struts.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="userActionForm" type="mystruts.userActionForm" />
</form-beans>
<action-mappings>
<action type="mystruts.userAction" scope="request" path="/userAction">
<forward name="view" path="/viewuser.jsp" />
<forward name="add" path="/adduser.jsp" />
<forward name="delview" path="/userAction.do?action=view" />
</action>
<action name="userActionForm" type="mystruts.user1Action" scope="request" path="/user1Action">
<forward name="view" path="/userAction.do?action=view" />
</action>
</action-mappings>
</struts-config>
运行程序:
启动tomcat
启动IE 在地址栏中输入 http://localhost:8080/mystruts
OK.....