zoukankan      html  css  js  c++  java
  • 【Struts2】新建一个Struts2工程,初步体验MVC

    实现目标

    地址栏输入http://localhost:88/Struts2HelloWorld/helloworld.jsp 

    输入用户名,交由http://localhost:88/Struts2HelloWorld/hello.action 处理

    返回到helloworld.jsp视图层,并显示刚才输入的值。


    步骤

    1、 新建工程

    MyElipse下新建一个叫struts2helloweb工程;

    2、 导入Jar包

    struts 2jarcopyWEB-INF/lib/目录下;

    或者在工程的.classpath文件中加入:

    <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.struts2.MYECLIPSE_STRUTS21_CORE"/>
    并刷新工程。

    3、配置Filter

    修改web.xml文件,配置Struts 2的核心Filter

    	<filter>
    		<filter-name>struts2</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>/*.action</url-pattern>
    	</filter-mapping>

    4、配置struts.xml

    src 目录下增加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>
    	<include file="struts-default.xml" />
    	<constant name="struts.i18n.encoding" value="utf-8" />
    	<package name="default" extends="struts-default">
    		<global-results>
    			<result name="login">login.jsp</result>
    			<result name="index">index.jsp</result>
    		</global-results>
    		<action name="UserAction" class="com.app.action.UserAction" />
    	</package>
    </struts>

    5、 实现控制器

    package com.app.hello;
    
    import com.opensymphony.xwork2.Action;
    //POJO(Plain Object Java Object 低侵入式)
    public class UserWorld implements Action{
        //获取页面上的控件,只需要要在这里注入对应的属性,提供setter和getter方法即可。(名称要完全一样)
        private User user;//action类德每个属性对应于页面中的某个控件属性
        private UserDAO userDAO = new UserDAO();
    
        // action默认执行方法
        public String execute() {
        	if(!userDAO.login(user.getUsername(), user.getPassword())) {
    	    	ActionContext ac = ActionContext.getContext();
    	    	ac.put("msg", "登陆信息有误"); //request.setAttribute("", );
        	} else {
        		return "index"; //转发到逻辑视图对应的页面
        	}
            return "login";
        }
    
    	public User getUser() {
    		return user;
    	}
    
    	public void setUser(User user) {
    		this.user = user;
    	}
        // http://localhost:8080/Struts2HelloWorld/hello!aliasAction.action
        public String aliasAction() {
            name = "";
            return "success";
        }
           
    }

    6、 修改struts.xml文件

    添加Action的映射和逻辑视图转向,在<struts></struts>中添加如下内容;

        <package name="default" extends="struts-default">
            <action name="hello" class="com.app.hello.HelloWorld">
                <result name="success">/helloworld.jsp</result>
            </action>
        </package>

    7、 添加JSP视图层

    取名helloworld.jsp

    <%@ page contentType="text/html; charset=utf-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>Say Hello</title>
        </head>
        <body>
            <h3>Say "Hello" to: </h3>
            <s:property value="name"/> ${name}
            <s:form action="hello">
                Name: <s:textfield name="user.username" />
                <s:submit />
            </s:form>
        </body>
    </html>

  • 相关阅读:
    BEC listen and translation exercise 16
    BEC listen and translation exercise 15
    BEC listen and translation exercise 14
    PyQt(Python+Qt)学习随笔:toolButton的popupMode属性
    PyQt(Python+Qt)学习随笔:实现toolButton与Action的关联
    PyQt(Python+Qt)学习随笔:Qt Designer中连接Action和槽函数
    PyQt(Python+Qt)学习随笔:Qt Designer中怎么给toolBar添加按钮
    PyQt(Python+Qt)学习随笔:快速理解Qt 中Action是什么
    PyQt(Python+Qt)实战:使用QCamera、QtMultimedia等实现摄像头拍照
    第15.13节 PyQt(Python+Qt)入门学习:Qt Designer的Spacers部件详解
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3303983.html
Copyright © 2011-2022 走看看