码迷,mamicode.com
首页 > 其他好文 > 详细

SSH实例(5)

时间:2015-06-07 17:27:04      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

在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>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.reload" value="true" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.custom.i18n.resources" value="globalMessages" />
    <constant name="struts.url.includeParams" value="none" />
    <constant name="struts.ui.theme" value="xhtml"></constant>
    <package name="struts2" extends="struts-default">
        <default-action-ref name="clasquery"></default-action-ref>
        <action name="clasquery" class="clasQueryAction">
            <result name="success">/query.jsp</result>    
        </action>    
        <action name="clas" class="clasAction">
            <result name="success" type="redirect">/clasquery.action</result>    
        </action>    
    </package>
</struts> 

ClasAction.java文件:

package com.school.action;

import com.school.entity.Clas;
import com.school.service.ClasService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ClasAction extends ActionSupport {
    
    private ClasService clasService;
    public void setClasService(ClasService clasService) {
        this.clasService = clasService;
    }
    
    private Clas clas;
    public String execute(){
        Clas c=new Clas();
        c.setName(clas.getName());
        c.setComment(clas.getComment());
        clasService.saveClas(c);
        return SUCCESS;
    }
    
    public Clas getClas() {
        return clas;
    }
    
    public void setClas(Clas Clas) {
        this.clas = Clas;
    }
}

ClasQueryAction.java文件:

package com.school.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.school.entity.Clas;
import com.school.service.ClasService;

@SuppressWarnings("serial")
public class ClasQueryAction extends ActionSupport {
    
    private ClasService clasService;
    
    // 设置业务逻辑组件
    public void setClasService(ClasService clasService) {
        this.clasService = clasService;
    }
    
    public String execute(){    
            List<Clas> claslist = clasService.findAll();
            // 将所有课程存放在request范围内
            ServletActionContext.getRequest().setAttribute("claslist", claslist);
            return SUCCESS;
    }
    
}

 

SSH实例(5)

标签:

原文地址:http://www.cnblogs.com/mstk/p/4558618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!