1
回答
关于SSH框架中的实现关联实体类后怎么通过关联关系添加实体类属性??
终于搞明白,存储TCO原来是这样算的>>>   

想在用户登录后展现用户的文章:现在jsp中输出

 

主要jsp代码:

​
<%@ page import="static org.apache.struts2.interceptor.DateTextFieldInterceptor.DateWord.s" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 
  Date: 2017/9/10
  Time: 9:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
  <h1>登录成功</h1>
  <table border="1" width="250">
    <tr>
      <td colspan="1" width="125">
      <s:property value="#session.existUser.uname"/>
      </td>
      <td colspan="1">
      <s:property value="#session.existUser.sex"/>
      </td>
    </tr>
    <tr>
      <td colspan="2">
    <textarea cols="40" rows="10">
      <s:property value="#session.existUser.udesc"/>
    </textarea>
      </td>
    </tr>
  </table>


  <table border="1" width="500">
    <tr>
      <textarea name="article123" id="article123" cols="30" rows="10">

        <!想在这里输出用户的文章>
        <s:property value="#session.existUser.articles.atext"/>
      </textarea>
    </tr>

  </table>



</body>
</html>

​

 

User.java

package com.rvelamen.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 林继锐 on 2017/9/9.
 */
public class User {
    private Integer uid;
    private String uname;
    private String password;
    private String sex;
    private String udesc;
    private Set articles=new HashSet(0);

    public Set getArticles() {
        return articles;
    }

    public void setArticles(Set articles) {
        this.articles = articles;
    }

    public String getUdesc() {
        return udesc;
    }

    public void setUdesc(String udesc) {
        this.udesc = udesc;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Artilcle.java

package com.rvelamen.domain;

/**
 * Created by 林继锐 on 2017/9/11.
 */
public class Article {
    private String atext;
    private String aid;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAtext() {
        return atext;
    }

    public void setAtext(String atext) {
        this.atext = atext;
    }
}

User.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.rvelamen.domain.User" table="user">
        <id name="uid" type="int">
            <column name="uid"/>
            <generator class="native"/>
        </id>
        <property name="sex" type="java.lang.String">
            <column name="sex"/>
        </property>
        <property name="udesc" type="text">
            <column name="udesc"/>
        </property>
        <property name="uname" type="java.lang.String">
            <column name="uname"/>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password"/>
        </property>
        <set name="articles" inverse="true">
            <key>
                <column name="ano"/>
            </key>
            <one-to-many class="com.rvelamen.domain.Article"/>
        </set>
    </class>
</hibernate-mapping>  

Article.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.rvelamen.domain.Article" table="article">
        <id name="aid" type="int">
            <column name="aid"/>
            <generator class="native"/>
        </id>
        <property name="atext" type="text">
            <column name="atext"/>
        </property>
        <many-to-one name="user" class="com.rvelamen.domain.User" fetch="select">
            <column name="ano"/>
        </many-to-one>
    </class>
</hibernate-mapping>  

struts.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="ssh" extends="struts-default" namespace="/">
        <action name="user_*" class="userAction" method="{1}">
            <result name="loginError">/loginUser.jsp</result>
            <result name="saveSuccess" type="redirect">/savesuccess.jsp</result>
            <result name="success" type="redirect">/index.jsp</result>
        </action>
    </package>
</struts>

UserAction

package com.rvelamen.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rvelamen.domain.Article;
import com.rvelamen.domain.User;
import com.rvelamen.service.UserService;
import com.rvelamen.service.impl.UserServiceImpl;

import java.util.List;

/**
 * Created by 林继锐 on 2017/9/9.
 */
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private static final long serialVersionUID=1L;

    private User user=new User();

    //获取模型
    @Override
    public User getModel() {
        return this.user;
    }

    private UserService userService;
    private Article article;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //用户保存方法
    public String save(){
        System.out.println("Action中的save方法执行了。。");
        userService.save(user);
        return "saveSuccess";
    }

    //用户登录方法
    public String login(){
//        System.out.println("Action中的密码:"+user.getPassword()+user.getUname());
        User existUser = userService.login(user);
        if (existUser==null){
//            System.out.println("密码或者账户错误" );
            this.addActionError("密码或者账户错误");
            return "loginError";
        }else{
            ActionContext.getContext().getSession().put("existUser",existUser);
            return SUCCESS;
        }

    }
}

 

<无标签>
举报
OSC_GotcLU
发帖于2周前 1回/34阅
顶部