struts2的简单流程

 

 sturcts获取前台数据 1.action获取  2.领域模型获取(vo) 3.model driver获取

 下面主要学习领域模型获取,既方便又简单易懂。

  1.  首先建立vo (com.wang.vo)User
package com.wang.vo;

public class User {
    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

  2.建立action(com.wang.action)LoginAction

package com.wang.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wang.vo.User;
public class LoginAction extends ActionSupport{
        private User user = null;
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    public String execute(){
        if(user.getName().trim().equalsIgnoreCase("admin")&&user.getPassword().equals("admin")){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    

}

  3.配置struct.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
        <package name="loginTest" extends="struts-default"  namespace="/">
        <action name="login" class="com.wang.action.LoginAction" >
        <result name="success">/success.jsp</result>
        <result name="error">/error.jsp</result>
        </action>
</package>
</struts>

  4.配置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">
  <display-name></display-name>   
<!-- sturct自带的过滤器,前端过滤--> <filter> <filter-name>struct2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struct2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

  5.前台jsp包括error.jsp、index.jsp、success.jsp

  注:index.jsp中action="login.action",login必须与sturct.xml中配置的login相同,这样才能正确的找到action

<%@ 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%>">
     </head>
  <body>
    <fieldset>
        <legend>登录</legend>
        <form action="login.action" method="post">
            姓名:<input type="text" name="user.name" /><br>密码<input type="password"
                name="user.password" /><br> <input type="submit" value="提交">
        </form>
    </fieldset>
  </body>
</html>

  success.jsp

  <%@ taglib uri="/struts-tags" prefix="c" %>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="c" %>
<%
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 'success.jsp' starting page</title>
     </head>
  <body>
    <h4>欢迎  <c:property value="user.name"/>登陆成功</h4>
  </body>
</html>

 

posted @ 2013-01-15 22:01  蜗牛的夏天  阅读(193)  评论(0编辑  收藏  举报