Web框架学习篇--Strust1(二) 实例

来源:互联网 发布:软件配置管理计划模板 编辑:程序博客网 时间:2024/05/29 19:07

一、引入相关jar包

  • antlr-2.7.2.jar
  • bsf-2.3.0.jar
  • commons-beanutils-1.8.0.jar
  • commons-chain-1.2.jar
  • commons-digester-1.8.jar
  • commons-fileupload-1.1.1.jar
  • commons-io-1.1.jar
  • commons-logging-1.0.4.jar
  • commons-validator-1.3.1.jar
  • jstl-1.0.2.jar
  • oro-2.0.8.jar
  • standard-1.0.6.jar
  • struts-core-1.3.10.jar
  • struts-el-1.3.10.jar
  • struts-extras-1.3.10.jar
  • struts-faces-1.3.10.jar
  • struts-mailreader-dao-1.3.10.jar
  • struts-scripting-1.3.10.jar
  • struts-taglib-1.3.10.jar
  • struts-tiles-1.3.10.jar

二、在web.xml 中配置struts

<pre name="code" class="html"><!--  配置乱码过滤器  -->  <filter>  <filter-name>jspPermission</filter-name>  <filter-class>com.hygj.cust.filter.JSPPermissionFilter</filter-class>  <init-param>  <param-name>encoding</param-name>  <param-value>UTF-8</param-value>  </init-param>  </filter>  <filter-mapping>  <filter-name>jspPermission</filter-name>  <url-pattern>*.jsp</url-pattern>  </filter-mapping>  <filter-mapping>  <filter-name>jspPermission</filter-name>  <url-pattern>*.do</url-pattern>  </filter-mapping>
<!-- 配置核心控制器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/config/struts-config.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>


三、在strust-config.xml 中配置 Action、 ActionForm、Forward

<struts-config><form-beans><form-bean name="loginActionForm"  type="com.hygj.cust.web.LoginActionForm"/></form-beans><action-mappings><action path="/loginAction" type="com.hygj.cust.web.LoginAction" name="loginActionForm" parameter="operAtt" scope="request"><forward name="succ" path="/succ.jsp"/><forward name="index" path="/index.jsp"/></action></action-mappings></struts-config>


四、相关类

1 Action

package com.hygj.cust.web;import java.io.PrintWriter;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.actions.DispatchAction;public class LoginAction extends DispatchAction {public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {/*String name = request.getParameter("name");System.out.println(name);response.setContentType("text/html;charset=utf-8");PrintWriter writer = response.getWriter();writer.write("你是萨比");writer.flush();writer.close();//return mapping.findForward("index");*/LoginActionForm actionForm = (LoginActionForm)form;PersionPo persion = actionForm.getPersion();Student student = actionForm.getStudent();String name1 = student.getName();String sex1 = student.getSex();String name = persion.getName();String sex = persion.getSex();System.out.println(name+"====="+sex);System.out.println(name1+"====="+sex1);persion.setName("褚厚起");persion.setSex("男");return mapping.findForward("succ");}}<strong></strong>

2 过滤器

package com.hygj.cust.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class JSPPermissionFilter implements Filter{private HttpServletRequest request;private HttpServletResponse response;private String encoding = "UTF-8";public JSPPermissionFilter() {}@Overridepublic void destroy() {System.out.println("过滤器结束");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain chain) throws IOException, ServletException {this.request = (HttpServletRequest) servletRequest;this.response = (HttpServletResponse) servletResponse;System.out.println(request.getRequestURI());if(encoding!=null){request.setCharacterEncoding(encoding);response.setContentType("text/html;charset="+encoding);}chain.doFilter(request, response);}@Overridepublic void init(FilterConfig config) throws ServletException {System.out.println("过滤器初始化");encoding = config.getInitParameter("encoding");System.out.println(encoding);}}

3 ActionForm


package com.hygj.cust.web;import org.apache.struts.action.ActionForm;public class LoginActionForm extends ActionForm{private static final long serialVersionUID = 1L;public LoginActionForm() {}private PersionPo persion = new PersionPo();private Student student = new Student();public PersionPo getPersion() {return persion;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public void setPersion(PersionPo persion) {this.persion = persion;}}

package com.hygj.cust.web;public class PersionPo {public PersionPo() {System.out.println("初始化");}private String name = "";private String sex = "";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}

package com.hygj.cust.web;public class Student {public Student() {// TODO Auto-generated constructor stub}private String name = "";private String sex = "";public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}

五、页面访问 及服务器数据返回

访问jsp页面并发起请求

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!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><script src="js/jquery.min.js"></script><script type="text/javascript">function ajax(){var form = $("#fm").serialize();alert(form);$.ajax({type:'post',url:'loginAction.do?operAtt=login',data:form,cache:false,success:function(data){alert(data);}});}</script><body><form id="fm" action="loginAction.do" method="post"><input type="hidden" name="operAtt" value="login">name:<input type="text" name="persion.name"><br>sex: <input type="text" name="persion.sex"><br>name1:<input type="text" name="student.name"><br>sex1:<input type="text" name="student.sex"><br><input type="submit" value="提交"><!-- <input type="button" onclick="ajax()" value="提交"/> --></form></body></html>
页面返回:
1 使用jsp中加入Java代码来实现页面接收
<%@page import="com.hygj.cust.web.LoginActionForm"%><%@page import="com.hygj.cust.web.PersionPo"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!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=uft-8"><title>Insert title here</title><%LoginActionForm form = (LoginActionForm)request.getAttribute("loginActionForm"); PersionPo persion = form.getPersion();String name = persion.getName();%></head><body><%=name %><form action="" name="" method="POST" ></form></body></html>

2 使用bean 标签来接收数据

<%@page import="com.hygj.cust.web.LoginActionForm"%><%@page import="com.hygj.cust.web.PersionPo"%><%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!--引入struts标签  --><%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><!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=uft-8"><title>Insert title here</title></head><body><!--bean:write常用的属性有如下几个: 1。name,用来指定属性的名字2。filter,用来指定是否屏蔽到属性值的HTML格式3。property,用来指定name所代表的对象的属性名字4。format,用来指定显示的时间,数字,日期等的格式--><bean:write name="loginActionForm" property="persion.name"/></form></body></html>




















0 0
原创粉丝点击