熟悉struts2MVC模式之用模型驱动传参的例子
2012-12-09 22:53
267 查看
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>
<?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>
相关文章推荐
- 熟悉struts2MVC模式之用属性驱动传参例子
- struts2基于模型驱动的文件上传(上传视频为例子)
- sax(用于处理XML事件驱动的推模型)解析例子
- Struts2的属性驱动与模型驱动的区别
- Struts2学习之Action接收参数方法二(用DomainModel接收参数(模型驱动))
- Struts2的属性驱动与模型驱动的区别
- Java模仿Struts2模型驱动的实现
- struts2 模型驱动Action属性 -标签 -Action属性不能赋值。
- struts2的模型驱动(推荐使用)、属性驱动模式
- 让JSON请求和参数请求一样,可以通过Struts2的模型驱动给Action的类成员变量赋值(字段驱动与模型驱动)
- Struts2的属性驱动与模型驱动的区别
- Struts2模型驱动
- Struts2的属性驱动原理和模型驱动原理
- 基于struts2的模型驱动示例
- struts2模型驱动获取表单的数据是空
- Struts2模型驱动
- 7、struts2 案例( 模型驱动、 防止表单重复提交--拦截器 、数据回显 、值栈 、 OGNL表达式综合运用)
- 4、struts2异常处理及全局异常与结果剖析、struts2模型驱动及分层体系架构
- Struts2(四)属性驱动和模型驱动
- struts2属性驱动,模型驱动