struts2启程1
struts2启程之基本配置、核心概念及原理
1、struts2执行需要的几个文件:
web.xml、struts.xml、jsp页面以及Action类
a.web.xml中添加struts2过滤器,
需要注意的是:
A、struts2现在是作为过滤器使用,需要的过滤器类是org.apache.struts2.dispatcher.FilterDispatcher
B、如果将url-pattern设置成 /* 后,表示所有的url都会使用这个过滤器
- xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <filter>
- <filter-name>wjl_strutsfilter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
- filter>
- <filter-mapping>
- <filter-name>wjl_strutsfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
b.struts.xml文件必须位于classes根目录下
需要注意的是:
A、struts.xml文件中的头部dtd声明部分可以直接拷贝下载下来的struts2提供的apps中所用的struts.xml文件的头部dtd声明部分
B、action的name属性值为jsp页面中form表单的action值,如这里jsp的form应该是action="login.action"
C、result的name属性值为Action类execute方法返回的字符串值,result之间的值是指执行晚execute后dispatche到哪个jsp页面
- xml version="1.0" encoding="UTF-8" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="wjl_struts" extends="struts-default">
- <action name="login" class="com.test.action.LoginAction">
- <result name="success">/result.jspresult>
- action>
- package>
- struts>
2、struts执行流程
通过jsp页面中的form的action,将表单提交到通过struts.xml文件配置的Action类中,在这个类中,通过set属性名的方式使得对应的属性获得从Jsp页面中传递过来的对应的表单元素的值,并通过执行该类的execute方法返回一个字符串,并在struts.xml文件对应的result列表中去寻找与此字符串对应的那个result值,如果找到了,那么将dispatche到该值所对应的jsp页面中,该页面通过jstl标签${ requestScope.username }的方式来获得Action类中的属性值,并显示。
3、示例代码:
- xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <filter>
- <filter-name>wjl_strutsfilter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
- filter>
- <filter-mapping>
- <filter-name>wjl_strutsfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
- xml version="1.0" encoding="UTF-8" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="wjl_struts" extends="struts-default">
- <action name="login" class="com.test.action.LoginAction">
- <result name="success">/result.jspresult>
- action>
- package>
- struts>
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <base href="">
- <title>My JSP 'login.jsp' starting pagetitle>
- <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">
- head>
- <body>
- <form action="login.action" method="post">
- username:<input type="text" name="username"><br/>
- password:<input type="password" name="password"><br/>
- <input type="submit" value="submit">
- form>
- body>
- html>
- <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <base href="">
- <title>My JSP 'result.jsp' starting pagetitle>
- <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">
- head>
- <body>
- username:${ requestScope.username }<br/>
- password:${ requestScope.password }
- body>
- html>
- package com.test.action;
- public class LoginAction {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String execute() throws Exception
- {
- return "success";
- }
- }
发表评论
- 浏览: 29760 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
我的相册
共 14 张
最近加入圈子
最新评论
-
ExtJs视频教程集锦
怎么ftp上还没有, 急死了!
-- by slmdyk -
EXTJS动态树的实现
不明白你说的意思
-- by yahaitt -
EXTJS动态树的实现
你好! 请问添加节点时, 提示需要验证: http://localhost:80 ...
-- by davy138 -
ExtJs想入门的请进-解读 ...
非常感谢!正在学extjs,正不知道该如何下手。对java script不熟悉, ...
-- by xt95 -
关于动态树的完整示例代码 ...
...
-- by yahaitt






评论排行榜