熟悉struts2MVC模式之用模型驱动传参的例子

来源:互联网 发布:男女对唱的网络歌曲 编辑:程序博客网 时间:2024/05/20 03:06

web.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping></web-app>

model层(pojo普通java类):

package com.bjsxt.model;

public class Stu {

 private int id;
 private String name;
 private String pwd;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPwd() {
  return pwd;
 }

 public void setPwd(String pwd) {
  this.pwd = pwd;
 }

}

control控制层(action类):

package com.bjsxt.action;

import com.bjsxt.model.Stu;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StuAction extends ActionSupport implements ModelDriven<Stu> {

 public Stu getStu() {
  return stu;
 }

 public void setStu(Stu stu) {
  this.stu = stu;
 }

 private Stu stu = new Stu();

 @Override
 public Stu getModel() {

  return stu;
 }

 @Override
 public String execute() throws Exception {

  System.out.println(stu.getId());
  System.out.println(stu.getName());
  System.out.println(stu.getPwd());
  return "stu";
 }
}

显示层(view)登陆页面:

<%@ 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 'stulogin.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>
        <h3 align="center">stu_info</h3>
     <form action="StuAction" method="post">
     ID号:<input type="text" name="id"><br>
     姓名:<input type="text" name="name"><br>
     密码:<input type="password" name="pwd"><br>
     <input type="submit" value="提交">
     <input type="reset" value="重置">
    </form>
  </body>
</html>

 

显示层(view)输出页面:

<%@ 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 'stu.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>
    <h3 align="center">stu_collect</h3>
    <p align="center">
    ID号:<%=request.getParameter("id") %><br>
          姓名:<%=request.getParameter("name") %><br>
          密码:<%=request.getParameter("pwd") %><br>
    </p>
  </body>
</html>

配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="a" namespace="/" extends="struts-default">
  <action name="User" class="com.bjsxt.action.User">
   <result name="user">user.jsp</result>
  </action>
  <action name="StuAction" class="com.bjsxt.action.StuAction">
   <result name="stu">stu.jsp</result>
  </action>
 </package>

 <constant name="struts.devMode" value="true"></constant>
 <constant name="struts.i18n.encoding" value="UTF-8"></constant>
</struts>   

 

 

 

原创粉丝点击