• SSH整合之spring整合struts2(续上)


    一、项目结构

    二、新建UserAction,继承自ActionSupport,并实现ModelDriven<User>接口;getModel方法是用来装配对象属性的;注意,userService字段名要和applicationContext.xml中配置的userservice bean中id名称一致

    package com.hjp.action;
    
    import com.hjp.domain.User;
    import com.hjp.service.UserService;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    /**
     * Created by JiaPeng on 2015/12/13.
     */
    public class UserAction extends ActionSupport implements ModelDriven<User> {
    
        User user = new User();
    
        @Override
        public User getModel() {
            return user;
        }
    
        private UserService userService;
    
        public void setUserService(UserService userService) {
            this.userService = userService;
        }
    
        public String register(){
            userService.register(user);
            return SUCCESS;
        }
    }
    UserAction

    三、新建index.jsp文件,代码如下

    <%--
      Created by IntelliJ IDEA.
      User: JiaPeng
      Date: 2015/12/13
      Time: 11:36
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <form method="post" action="${pageContext.request.contextPath}/userAction_register">
        用户名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        年龄:<input type="text" name="age"/><br/>
        <input type="submit" value="注册"/>
    </form>
    </body>
    </html>
    index.jsp

    四、新建struts.xml文件,及message.jsp文件

    <?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>
        <!--常量-->
        <constant name="struts.devMode" value="true"></constant>
        <!---->
        <package name="hello" namespace="/" extends="struts-default">
            <action name="userAction_*" class="com.hjp.action.UserAction" method="{1}">
                <result name="success">/message.jsp</result>
            </action>
        </package>
    
    </struts>
    struts.xml
    <%--
      Created by IntelliJ IDEA.
      User: JiaPeng
      Date: 2015/12/13
      Time: 16:23
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    成功!
    </body>
    </html>
    message.jsp

    五、配置web.xml文件,主要是配置spring和struts

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <!--spring-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--struts-->
        <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>
    web.xml
  • 相关阅读:
    KDrive 與 Embedded Linux
    windows内存管理初探
    开源方案
    boot time 优化
    psplash
    linux下操纵大于2G文件
    【技术贴】Windows图片和传真查看器打开图片慢,正在生成预览的解决办法!
    【转】c++.primer.plus.第五版.中文版[下载]
    【技术贴】魂斗罗坦克Normal Tanks第五关以及第5、6、7、关的LICENCE CODE的查
    【转】奇文共欣赏,疑义相与析:原文转载《电脑维护技巧》(N条举措N条理由)并请大家交流研讨
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/5043456.html
Copyright © 2020-2023  润新知